Created
June 4, 2012 13:11
-
-
Save Dynyx/2868272 to your computer and use it in GitHub Desktop.
Bit.ly URL shortening
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
using System.Xml; | |
namespace MyApp | |
{ | |
public class Bitly | |
{ | |
public static string Shorten(string url) | |
{ | |
return Shorten( | |
ConfigurationManager.AppSettings["bitlyUsername"], | |
ConfigurationManager.AppSettings["bitlyAPIKey"], | |
url, | |
"Shorten"); | |
} | |
private static string Shorten(string username, string apiKey, string input, string type) | |
{ | |
string Btype; | |
string Xtype; | |
string Itype; | |
switch (type) | |
{ | |
case "Shorten": | |
Btype = "shorten"; | |
Xtype = "shortUrl"; | |
Itype = "longUrl"; | |
break; | |
case "MetaH": | |
Btype = "info"; | |
Xtype = "htmlMetaDescription"; | |
Itype = "hash"; | |
break; | |
case "MetaU": | |
Btype = "info"; | |
Xtype = "htmlMetaDescription"; | |
Itype = "shortUrl"; | |
break; | |
case "ExpandH": | |
Btype = "expand"; | |
Xtype = "longUrl"; | |
Itype = "hash"; | |
break; | |
case "ExpandU": | |
Btype = "expand"; | |
Xtype = "longUrl"; | |
Itype = "shortUrl"; | |
break; | |
case "ClicksU": | |
Btype = "stats"; | |
Xtype = "clicks"; | |
Itype = "shortUrl"; | |
break; | |
case "ClicksH": | |
Btype = "stats"; | |
Xtype = "clicks"; | |
Itype = "hash"; | |
break; | |
case "UserU": | |
Btype = "info"; | |
Xtype = "shortenedByUser"; | |
Itype = "shortUrl"; | |
break; | |
case "UserH": | |
Btype = "info"; | |
Xtype = "shortenedByUser"; | |
Itype = "hash"; | |
break; | |
default: | |
return ""; | |
} | |
var url = new StringBuilder(); //Build a new string | |
url.Append("http://api.bit.ly/"); //Add base URL | |
url.Append(Btype); | |
url.Append("?version=2.0.1"); //Add Version | |
url.Append("&format=xml"); | |
url.Append("&"); | |
url.Append(Itype); | |
url.Append("="); | |
url.Append(input); //Append longUrl from input | |
url.Append("&login="); //Add login "Key" | |
url.Append(username); //Append login from input | |
url.Append("&apiKey="); //Add ApiKey "Key" | |
url.Append(apiKey); //Append ApiKey from input | |
var request = WebRequest.Create(url.ToString()); //prepare web request | |
var responseStream = new StreamReader(request.GetResponse().GetResponseStream()); //prepare responese holder | |
var response = responseStream.ReadToEnd(); //fill up response | |
responseStream.Close(); //Close stream | |
var data = response; //Turn it into a string | |
var newdata = XmlParseGeneral(data, Xtype); //parse the XML | |
return newdata == "Error" ? string.Empty : newdata; | |
} | |
private static string XmlParseGeneral(string url, string type) //XML parse Function | |
{ | |
var xmlrt1 = new XmlTextReader(new StringReader(url)); | |
while (xmlrt1.Read()) | |
{ | |
string strNodeType = xmlrt1.NodeType.ToString(); | |
string strName = xmlrt1.Name; | |
if (strNodeType != "Element" || strName != type) continue; | |
xmlrt1.Read(); | |
return xmlrt1.Value; //Return output | |
} return "";// end while | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment