首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在ASP.NET 2.0中使用Bit.ly应用程序接口

在ASP.NET 2.0中使用Bit.ly应用程序接口
EN

Stack Overflow用户
提问于 2010-10-20 23:02:09
回答 3查看 3.6K关注 0票数 5

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-10-20 23:30:27

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

我还没有测试过(抱歉),但它可能会在这段时间内有所帮助,我会把它整理出来,让它对C#风格更友好一些。

代码语言:javascript
运行
复制
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/

票数 5
EN

Stack Overflow用户

发布于 2011-08-03 00:21:15

我从tim那里找到了答案,这是非常可靠的。我需要一个vb.net版本,所以将它从C#转换回来-我想这可能会对某些人有帮助。看起来bit.ly链接已经改变了;不确定是否还需要这个版本;添加了一些错误处理,以防你传入一个错误的url。

代码语言:javascript
运行
复制
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
票数 1
EN

Stack Overflow用户

发布于 2013-09-06 00:43:50

BitlyIn有一个更短的版本

代码语言:javascript
运行
复制
    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());
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3979271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档