asp.net.net-2.0bit.ly

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


Solution

  • 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/