How to use Tiny-URL API for mvc4 in asp.net

I want to use the Tiny-URL API in MVC4, any ideas how I can use this API in my solution?

I referenced its documentation, but it was in a PHP document link

+4
source share
1 answer

You can use the same code as in this answer , but with different uri.

First of all, you need to request the API key and set the variable accordingly apikey. Then select the provider string that you will use from the API documentation (I use 0_mkfor the 0.mkprovider in the example below).

You can then compose the URL and make the request as follows:

string yourUrl = "http://your-site.com/your-url-for-minification";
string apikey = "YOUR-API-KEY-GOES-HERE";
string provider = "0_mk"; // see provider strings list in API docs
string uriString = string.Format(
    "http://tiny-url.info/api/v1/create?url={0}&apikey={1}&provider={2}&format=text",
    yourUrl, apikey, provider);

System.Uri address = new System.Uri(uriString);
System.Net.WebClient client = new System.Net.WebClient();
try
{
    string tinyUrl = client.DownloadString(address);
    Console.WriteLine(tinyUrl);
}
catch (Exception ex)
{
    Console.WriteLine("network error occurred: {0}", ex);
}

According to the docs, the default format format=text, so you do not need to specify it. You can also use format=xmlor format=json, if you want, but then you will need to parse the output (and you will answer in the field stateand be able to handle errors).

: .NET 4.5 URL WebClient.DownloadStringAsync() ( , async):

...
string tinyUrl = await client.DownloadStringAsync(uriString);
...
0

All Articles