在 C#.NET/VB.NET/PHP 中使用 Tinyit.cc 链接缩短 API 服务





0/5 (0投票)
本文详细介绍了如何在基于 C#/VB/PHP 的 Web 应用程序中使用 Tinyit.cc 链接缩短和跟踪服务 API。Tinyit.cc 是最好的 URL 缩短服务之一,提供免费 API 来访问其无垃圾邮件的链接缩短和跟踪服务。

引言
本文介绍了如何使用 `Tinyit.cc` 链接缩短和跟踪 API。文章提供了不同语言(C#、VB、PHP)的解释。读完本文,您将已在 Web 应用程序中实现了 `Tinyit.cc` 链接缩短 Web 服务(API)。您还将学习如何使用简单的 `HttpWebRequest` 和 `HttpWebResponse` 方法在应用程序中调用 Web 服务。这两个方法用于通过 HTTP 在 Web 上发送信息(请求数据)并获取响应(获取数据)。
简单介绍一下链接缩短服务。我们看到链接缩短服务的数量不断增加,它们发挥着有用的作用,即将长 URL 转换为简短实用的 tiny URL,这些 URL 易于记忆,并且易于发布在网站、博客、论坛、电子邮件等地方。这意味着:http://codingzone.net/code/candcplusplus/program-to-define-and-initialize-an-array-and-display-even-elements-stored-in-it-using-cc,这是我们的长 URL,而 http://tinyit.cc/array,这是我们这个长 URL 的简短 tiny URL。简单而有用。而 `Tinyit.cc` 是一个简单而强大的 URL 缩短服务,您可以通过它将长链接转换为简短的 tiny 链接,即时自定义并与朋友分享,并获得您的 tiny 链接的全面点击统计信息,包括唯一点击次数、总点击次数以及点击您链接的用户的国家、浏览器和操作系统信息。`Tinyit.cc` 在重定向到目标 URL 之前,还会对照 Google 安全浏览网站列表、phishtank 钓鱼列表、垃圾邮件列表、SURBL 列表和 `Tinyit.cc` 阻止的网站列表检查每个 URL。因此,您创建的短 URL 是安全的短 URL。现在,让我们来了解一下 Web 服务的基础知识以及如何在我们的 Web 应用程序中调用它们。
背景
首先简单介绍一下 Web 服务。Web 服务是两个电子设备在网络上通信的一种方式。程序员出于各种目的实现 Web 服务,例如提供中间件来访问远程数据库,提供中间件来通过 Web 访问应用程序软件等。Web 服务基本上包含许多过程和函数,远程用户可以调用这些过程和函数来访问服务。因此,一个基本的计算器 Web 服务将具有不同的远程过程,例如 `Add(number1,number2)`、`Subtract(number1,number2)` 等,如果用户在其应用程序中需要计算器类型的功能,就可以调用这些过程。
现在,如何调用 Web 服务。我们可以使用简单的 `HttpWebRequest` 和 `HttpWebResponse` 方法在我们的 Web 应用程序中调用 Web 服务。这两个方法用于通过 HTTP 在 Web 上发送信息(请求数据)并获取响应(获取数据)。有关不同语言的实际实现,请继续阅读。
Using the Code
`Tinyit.cc` 提供了一个易于使用的 Web API,用户可以通过该 API 创建短链接并获取短链接的点击次数。我们将用三种语言实现 API - C#、VB 和 PHP。
访问 `Tinyit.cc` 链接缩短 API 的 Web URL 是
http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY"
在这里,`LONG_URL_WITHOUT_HTTP` 是需要缩短的长 URL,不带“`http://`”前缀,`USERNAME` 是您的 `Tinyit.cc` 用户名,`APIKEY` 是您的 `Tinyit.cc` API 密钥。请在此处获取新的 API 密钥。
以下是使用 `Tinyit.cc` API 创建短 URL 的简单 C#.NET 代码。
Uri uri = new Uri("http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&
user=USERNAME&api=APIKEY"); //The API access URL containing the long URL
string data = "field-keywords=ASP.NET 2.0";
if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request = (HttpWebRequest)
HttpWebRequest.Create(uri); //New web request object for our long URL
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)
request.GetResponse(); //Web response object to get the results
StreamReader reader = new StreamReader(response.GetResponseStream());
string tinyccurl = reader.ReadToEnd(); //Our tiny.cc shortlink
response.Close();
Response.Write(tinyccurl);
}
因此,使用简单的 `HttpWebRequest` 和 `HttpWebResponse` 方法,我们已在应用程序中实现了 `Tinyit.cc` 链接缩短 API。
以下是使用 `Tinyit.cc` API 创建短 URL 的简单 VB.NET 代码。
Dim data As String = "field-keywords=ASP.NET 2.0"
Dim Uri As New Uri("http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&_
user=USERNAME&api=APIKEY") 'The API access URL containing the long URL
If Uri.Scheme = Uri.UriSchemeHttp Then
Dim request As HttpWebRequest = _
HttpWebRequest.Create(Uri) 'New web request object for our long URL
request.Method = WebRequestMethods.Http.Post
request.ContentLength = data.Length
request.ContentType = "application/x-www-form-urlencoded"
Dim writer As New StreamWriter(request.GetRequestStream)
writer.Write(data)
writer.Close()
Dim oResponse As HttpWebResponse = _
request.GetResponse() 'Web response object to get the results
Dim reader As New StreamReader(oResponse.GetResponseStream())
Dim tinyiturl As String = reader.ReadToEnd()
oResponse.Close()
Response.Write(tinyiturl)
End If
以下是使用 `Tinyit.cc` API 创建短 URL 的简单 PHP 代码。
$url = "http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY";
$resource = fopen( $url, 'r' );
$tinyccurl = '';
while (!feof($resource)) {
$tinyccurl .= fread($resource, 1);
}
fclose($resource);
echo $tinyccurl; //tiny.cc URL
获取短 URL 的点击次数
获取短链接点击次数的 Web URL 是
http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY&getclicks=1"
请注意我们添加到 API 请求 URL 的新开关 - '`getclicks=1`'。这个开关表示我们感兴趣的是获取短链接的点击次数,而不是创建新的短链接。非常简单。因此,代码基本保持不变,只是我们需要更改 API 访问 URL。
以下是获取短 URL 点击次数的简单 C#.NET 代码
Uri uri = new Uri("http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&
user=USERNAME&api=APIKEY&getclicks=1"); //The API access URL containing the long URL
string data = "field-keywords=ASP.NET 2.0";
if (uri.Scheme == Uri.UriSchemeHttp)
{
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(uri); //New web request object
//for our long URL
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response =
(HttpWebResponse)request.GetResponse(); //Web response object
//to get the results
StreamReader reader = new StreamReader(response.GetResponseStream());
string hits = reader.ReadToEnd(); //Our hits for the shortlink
response.Close();
Response.Write(tinyccurl);
}
因此,使用简单的 `HttpWebRequest` 和 `HttpWebResponse` 方法,我们已在应用程序中实现了 `Tinyit.cc` 链接缩短 API。
以下是获取短 URL 点击次数的简单 VB.NET 代码
Dim data As String = "field-keywords=ASP.NET 2.0"
Dim Uri As New Uri("http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&_
user=USERNAME&api=APIKEY&getclicks=1") 'The API access URL containing the long URL
If Uri.Scheme = Uri.UriSchemeHttp Then
Dim request As HttpWebRequest = _
HttpWebRequest.Create(Uri) 'New web request object for our long URL
request.Method = WebRequestMethods.Http.Post
request.ContentLength = data.Length
request.ContentType = "application/x-www-form-urlencoded"
Dim writer As New StreamWriter(request.GetRequestStream)
writer.Write(data)
writer.Close()
Dim oResponse As HttpWebResponse = _
request.GetResponse() 'Web response object to get the results
Dim reader As New StreamReader(oResponse.GetResponseStream())
Dim hits As String = reader.ReadToEnd() 'Our hits for the shortlink
oResponse.Close()
Response.Write(tinyiturl)
End If
以下是获取短 URL 点击次数的简单 PHP 代码
$url = "http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&
user=USERNAME&api=APIKEY&getclicks=1";
$resource = fopen( $url, 'r' );
$tinyccurl = '';
while (!feof($resource)) {
$tinyccurl .= fread($resource, 1);
}
fclose($resource);
echo $tinyccurl; //tiny.cc URL
非常简单快捷。您通过 API 创建的所有短链接都将在您的 `Tinyit.cc` 用户控制面板中可用。您可以从此处访问链接统计信息。
历史
- 2011 年 4 月 4 日:初始发布