2010-10-20 4 views

Antwort

5

ich wirklich schnell konvertieren getan haben zeigen können aus einer Antwort, die ich in VB gefunden habe.

Ich habe dies nicht getestet (sorry), aber es kann in der Zwischenzeit eine Hilfe sein, und ich sortiere es, um ein bisschen mehr C# Stil freundlich zu sein.

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

Originalcode von hier genommen - http://www.dougv.com/blog/2009/07/02/shortening-urls-with-the-bit-ly-api-via-asp-net/

0

fand ich die Antwort von tim und es ist ziemlich solide. Ich brauchte eine vb.net-Version, also konvertierte ich sie von C# zurück - ich dachte, das könnte jemandem helfen. Es scheint, dass sich der Link bit.ly geändert hat; nicht sicher, ob die Version mehr benötigt wird; eine kleine Fehlerbehandlung hinzugefügt, falls Sie eine schlechte URL übergeben.

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

gibt es etwas kürzere Version 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()); 
    }