4

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

Prashant
  • 966
  • 9
  • 26
Archana
  • 376
  • 2
  • 12
  • This is a duplicate of: http://stackoverflow.com/a/366149/1726419 – yossico Oct 29 '14 at 08:10
  • 1
    I want using http://tiny-url.info/api/v1/create/ api ...This is documentation http://tiny-url.info/open_api.html and when i am using it gives me an error of invalid url. please help. – Archana Oct 29 '14 at 14:33
  • I'd highly recommend having a look at http://restsharp.org/. I've used it in several projects and handles converting XML/JSON encoded responses to objects :) – Dan Apr 28 '15 at 12:53

1 Answers1

0

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);
...
Community
  • 1
  • 1
hal
  • 1,705
  • 1
  • 22
  • 28