加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在ASP.NET 2.0中使用Bit.ly API

发布时间:2020-12-16 06:57:56 所属栏目:asp.Net 来源:网络整理
导读:嘿,我想知道是否有人能指出我如何在ASP.NET 2.0中使用Bit.ly API的一些例子 解决方法 我从VB中找到的答案中做了一个非常快速的转换. 我没有测试过这个(对不起),但在此期间它可能会有所帮助,我会把它分类为更友好的C#风格. public static string BitlyIt(stri
嘿,我想知道是否有人能指出我如何在ASP.NET 2.0中使用Bit.ly API的一些例子

解决方法

我从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/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读