MrMistreater

Bit.ly URL Shortening

May 14th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System.Configuration;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Xml;
  6.  
  7. namespace MyApp
  8. {
  9.     public class Bitly
  10.     {
  11.         public static string Shorten(string url)
  12.         {
  13.             return Shorten(
  14.                 ConfigurationManager.AppSettings["bitlyUsername"],
  15.                 ConfigurationManager.AppSettings["bitlyAPIKey"],
  16.                 url,
  17.                 "Shorten");
  18.         }
  19.  
  20.         private static string Shorten(string username, string apiKey, string input, string type)
  21.         {
  22.             string Btype;
  23.             string Xtype;
  24.             string Itype;
  25.  
  26.             switch (type)
  27.             {
  28.                 case "Shorten":
  29.                     Btype = "shorten";
  30.                     Xtype = "shortUrl";
  31.                     Itype = "longUrl";
  32.                     break;
  33.                 case "MetaH":
  34.                     Btype = "info";
  35.                     Xtype = "htmlMetaDescription";
  36.                     Itype = "hash";
  37.                     break;
  38.                 case "MetaU":
  39.                     Btype = "info";
  40.                     Xtype = "htmlMetaDescription";
  41.                     Itype = "shortUrl";
  42.                     break;
  43.                 case "ExpandH":
  44.                     Btype = "expand";
  45.                     Xtype = "longUrl";
  46.                     Itype = "hash";
  47.                     break;
  48.                 case "ExpandU":
  49.                     Btype = "expand";
  50.                     Xtype = "longUrl";
  51.                     Itype = "shortUrl";
  52.                     break;
  53.                 case "ClicksU":
  54.                     Btype = "stats";
  55.                     Xtype = "clicks";
  56.                     Itype = "shortUrl";
  57.                     break;
  58.                 case "ClicksH":
  59.                     Btype = "stats";
  60.                     Xtype = "clicks";
  61.                     Itype = "hash";
  62.                     break;
  63.                 case "UserU":
  64.                     Btype = "info";
  65.                     Xtype = "shortenedByUser";
  66.                     Itype = "shortUrl";
  67.                     break;
  68.                 case "UserH":
  69.                     Btype = "info";
  70.                     Xtype = "shortenedByUser";
  71.                     Itype = "hash";
  72.                     break;
  73.                 default:
  74.                     return "";
  75.             }
  76.  
  77.             var url = new StringBuilder();              //Build a new string
  78.             url.Append("http://api.bit.ly/");           //Add base URL
  79.             url.Append(Btype);
  80.             url.Append("?version=2.0.1");               //Add Version
  81.             url.Append("&format=xml");
  82.             url.Append("&");
  83.             url.Append(Itype);
  84.             url.Append("=");
  85.             url.Append(input);                          //Append longUrl from input
  86.             url.Append("&login=");                      //Add login "Key"
  87.             url.Append(username);                       //Append login from input
  88.             url.Append("&apiKey=");                     //Add ApiKey "Key"
  89.             url.Append(apiKey);                         //Append ApiKey from input
  90.             var request = WebRequest.Create(url.ToString()); //prepare web request
  91.             var responseStream = new StreamReader(request.GetResponse().GetResponseStream()); //prepare responese holder
  92.             var response = responseStream.ReadToEnd();  //fill up response
  93.             responseStream.Close();                     //Close stream
  94.  
  95.             var data = response; //Turn it into a string
  96.             var newdata = XmlParseGeneral(data, Xtype); //parse the XML
  97.  
  98.             return newdata == "Error" ? string.Empty : newdata;
  99.         }
  100.  
  101.         private static string XmlParseGeneral(string url, string type)    //XML parse Function
  102.         {
  103.             var xmlrt1 = new XmlTextReader(new StringReader(url));
  104.  
  105.             while (xmlrt1.Read())
  106.             {
  107.                 string strNodeType = xmlrt1.NodeType.ToString();
  108.                 string strName = xmlrt1.Name;
  109.  
  110.                 if (strNodeType != "Element" || strName != type) continue;
  111.                 xmlrt1.Read();
  112.  
  113.                 return xmlrt1.Value; //Return output
  114.             } return "";// end while
  115.         }
  116.     }
  117. }
Add Comment
Please, Sign In to add comment