Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Bit.ly API in ASP.NET 2.0

Hey I was wondering if anyone can point me to some example on how to use Bit.ly API in ASP.NET 2.0

like image 536
StevieB Avatar asked Oct 20 '10 15:10

StevieB


1 Answers

I've done a really quick convert from an answer I found in VB.

I haven't tested this (sorry) but it may be of some help in the meantime, and I will sort it out to be a bit more C# style friendly.

public static string BitlyIt(string user, string apiKey, string strLongUrl)
{
   StringBuilder uri = new StringBuilder("http://api.bit.ly/shorten?");

   uri.Append("version=2.0.1");

   uri.Append("&format=xml");
   uri.Append("&longUrl=");
   uri.Append(HttpUtility.UrlEncode(strLongUrl));
   uri.Append("&login=");
   uri.Append(HttpUtility.UrlEncode(user));
   uri.Append("&apiKey=");
   uri.Append(HttpUtility.UrlEncode(apiKey));

   HttpWebRequest request = WebRequest.Create(uri.ToString()) as HttpWebRequest;
   request.Method = "GET";
   request.ContentType = "application/x-www-form-urlencoded";
   request.ServicePoint.Expect100Continue = false;
   request.ContentLength = 0;
   WebResponse objResponse = request.GetResponse();
   XmlDocument objXML = new XmlDocument();
   objXML.Load(objResponse.GetResponseStream());

   XmlNode nShortUrl = objXML.SelectSingleNode("//shortUrl");

   return nShortUrl.InnerText;
}

Original code taken from here - http://www.dougv.com/blog/2009/07/02/shortening-urls-with-the-bit-ly-api-via-asp-net/

like image 156
tim Avatar answered Nov 15 '22 04:11

tim