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

发布于 2024-09-28 07:29:15 字数 61 浏览 3 评论 0原文

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

Hey I was wondering if anyone can point me to some example on how to use Bit.ly API in ASP.NET 2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幻梦 2024-10-05 07:29:16

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

there is a bit shorter version of 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());
    }
傲娇萝莉攻 2024-10-05 07:29:16

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

Migrating from v3 to v4 of the Bitly API - Bitly V4 code for ASP.NET Applications

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;
}
雪若未夕 2024-10-05 07:29:15

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

I've done a really quick convert from an answer I found in VB.

I haven't tested this (sorry) but it may be of some help in the meantime, and I will sort it out to be a bit more C# style friendly.

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

Original code taken from here -
http://www.dougv.com/blog/2009/07/02/shortening-urls-with-the-bit-ly-api-via-asp-net/

半岛未凉 2024-10-05 07:29:15

我从蒂姆那里找到了答案,它非常可靠。我需要一个 vb.net 版本,因此将其从 C# 转换回来 - 我认为这可能会对某人有所帮助。 bit.ly 链接似乎已更改;不确定该版本是否还需要;添加了一些错误处理,以防您传入错误的网址。

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

I found the answer from tim and it's pretty solid. I needed a vb.net version so converted it back from C# - I figured this may help someone. It appears the the bit.ly link has changed; not sure if the version is necessary anymore; added a little error handling in case you pass in a bad 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文