2010-10-20 12 views

risposta

5

Ho fatto un molto veloce convertito da una risposta che ho trovato in VB.

Non ho provato questo (scusa) ma potrebbe essere di aiuto nel frattempo, e lo sistemerò per essere un po 'più amichevole in stile C#.

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; 
} 

Codice originale tratto da qui - http://www.dougv.com/blog/2009/07/02/shortening-urls-with-the-bit-ly-api-via-asp-net/

0

Ho trovato la risposta di Tim ed è abbastanza solido. Avevo bisogno di una versione di vb.net così convertita da C# - ho pensato che questo potesse aiutare qualcuno. Sembra che il link bit.ly sia cambiato; non sono sicuro che la versione sia più necessaria; aggiunto un po 'di gestione degli errori nel caso in cui si passa in un brutto url.

Public Shared Function BitlyIt(ByVal strLongUrl As String) As String 

    Dim uri As New StringBuilder("http://api.bitly.com/v3/shorten?") 

    'uri.Append("version=2.0.1") 'doesnt appear to be required 

    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)) 

    Dim request As HttpWebRequest = TryCast(WebRequest.Create(uri.ToString()), HttpWebRequest) 
    request.Method = "GET" 
    request.ContentType = "application/x-www-form-urlencoded" 
    request.ServicePoint.Expect100Continue = False 
    request.ContentLength = 0 

    Dim objResponse As WebResponse = request.GetResponse() 

    Dim myXML As New StreamReader(objResponse.GetResponseStream()) 
    Dim xr = XmlReader.Create(myXML) 
    Dim xdoc = XDocument.Load(xr) 

    If xdoc.Descendants("status_txt").Value = "OK" Then 

     Return xdoc.Descendants("url").Value 

    Else 

     Return "Error " & "ReturnValue: " & xdoc.Descendants("status_txt").Value 

    End If 

End Function 
0

c'è la versione un po 'più corta di BitlyIn

public static string BitlyEncrypt2(string user, string apiKey, string pUrl) 
    { 
     string uri = "http://api.bit.ly/shorten?version=2.0.1&format=txt" + 
      "&longUrl=" + HttpUtility.UrlEncode(pUrl) + 
      "&login=" + HttpUtility.UrlEncode(user) + 
      "&apiKey=" + HttpUtility.UrlEncode(apiKey); 

     HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest; 
     request.Method = "GET"; 
     request.ContentType = "application/x-www-form-urlencoded"; 
     request.ServicePoint.Expect100Continue = false; 
     request.ContentLength = 0; 

     return (new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd()); 
    }