I want to use Tiny-URL API in MVC4, any ideas how can I consume that API in my solution?
I refereed its documentation but it was in PHP document link
I want to use Tiny-URL API in MVC4, any ideas how can I consume that API in my solution?
I refereed its documentation but it was in PHP document link
You can use same code as in this answer, but with different uri.
First of all, you need to request API key and set apikey
variable accordingly. Then select provider string which you will use from API docs (I use 0_mk
for 0.mk
provider in example below).
Then you can compose url and make request like this:
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 is format=text
, so you don't need to specify it. You can also use format=xml
or format=json
if you want, but you'll need to parse output then (and you'll have state
field in response and may process errors).
UPDATE: With .NET 4.5 to asynchronously get tiny url await keyword may be used with WebClient.DownloadStringAsync()
function (you should do this in function, marked with async keyword):
...
string tinyUrl = await client.DownloadStringAsync(uriString);
...