【问题标题】:Using Bit.ly API in ASP.NET 2.0在 ASP.NET 2.0 中使用 Bit.ly API
【发布时间】:2010-10-20 15:02:08
【问题描述】:

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

【问题讨论】:

    标签: asp.net .net-2.0 bit.ly


    【解决方案1】:

    我已经从我在 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/

    【讨论】:

      【解决方案2】:

      我从 tim 那里找到了答案,它非常可靠。我需要一个 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
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-06-12
            • 1970-01-01
            • 2015-01-06
            • 2010-11-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多