5

嘿,我想知道是否有人可以指出一些关于如何在 ASP.NET 2.0 中使用 Bit.ly API 的示例

4

4 回答 4

5

我已经从我在 VB 中找到的答案进行了非常快速的转换。

我没有对此进行测试(抱歉),但在此期间它可能会有所帮助,我会对其进行整理,使其对 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;
}

从这里获取的原始代码 - http://www.dougv.com/blog/2009/07/02/shortening-urls-with-the-bit-ly-api-via-asp-net/

于 2010-10-20T15:30:27.193 回答
1

我从蒂姆那里找到了答案,它非常可靠。我需要一个 vb.net 版本,所以将它从 C# 转换回来 - 我认为这可能会对某人有所帮助。看来 bit.ly 链接已更改;不确定是否需要该版本;添加了一些错误处理,以防您传入错误的 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
于 2011-08-02T16:21:14.690 回答
0

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());
    }
于 2013-09-05T16:43:49.507 回答
0

Bitly API 从 v3 迁移到 v4 -适用于 ASP.NET 应用程序的 Bitly V4 代码

public string Shorten(string groupId, string token, string longUrl)
{
//string post = "{\"group_guid\": \"" + groupId + "\", \"long_url\": \"" + longUrl + "\"}";
string post = "{ \"long_url\": \"" + longUrl + "\"}";// If you've a free account.
string shortUrl = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api-ssl.bitly.com/v4/shorten");

try
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentLength = post.Length;
request.ContentType = "application/json";
request.Headers.Add("Cache-Control", "no-cache");
request.Host = "api-ssl.bitly.com";
request.Headers.Add("Authorization", "Bearer " + token);

using (Stream requestStream = request.GetRequestStream())
{
byte[] postBuffer = Encoding.ASCII.GetBytes(post);
requestStream.Write(postBuffer, 0, postBuffer.Length);
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
string json = responseReader.ReadToEnd();
shortUrl = Regex.Match(json, @"""link"": ?""(?[^,;]+)""").Groups["link"].Value;

//return Regex.Match(json, @"{""short_url"":""([^""]*)""[,}]").Groups[1].Value;
}
}
}
}
catch (Exception ex)
{
LogManager.WriteLog(ex.Message);
}

if (shortUrl.Length > 0) // this check is if your bit.ly rate exceeded
return shortUrl;
else
return longUrl;
}
于 2020-04-07T17:24:24.073 回答