Consuming Tinyit.cc Link Shortening API Service in C#.NET/VB.NET/PHP
- Download source ASP - C#.NET - 3.36 KB
- Download source ASP - VB.NET - 3.27 KB
- Download source PHP - 343 B

Introduction
This article explains how to consume the Tinyit.cc
link shortening and tracking API. Different explanations for different languages (C#, VB, PHP) have been given. By the end of this article, you would have implemented the Tinyit.cc
link shortening web service (API) in your web application. You would have also learnt the basics of how to call web services in your applications using simple HttpWebRequest
and HttpWebResponse
methods. These are the two methods which are used to send information (request for data) over the web and get a response (fetch data) via HTTP.
A little about what link shorteners actually are. We have seen a rise in number of link shortening services and they are doing a useful job, they are converting long URLs into small useful tiny URLs which are easy to remember and easy to put in websites, blogs, forums, Email, etc. So what this means is this: http://codingzone.net/code/candcplusplus/program-to-define-and-initialize-an-array-and-display-even-elements-stored-in-it-using-cc, which is our long URL and http://tinyit.cc/array, which is our short tiny URL for that long URL. Simple, yet useful. And Tinyit.cc
is a simple yet powerful URL shortener through which you can convert long links to short tiny links, customize and share them instantly with your friends and get comprehensive click statistics for your tiny links, viz. unique hits, total hits and along with country, browser and operating system information of the user who clicked your link. Tinyit.cc
also checks each URL before redirecting to target URL against Google safe browsing list of websites, phishtank phishing list, spamming list, SURBL list and Tinyit.cc
blocked websites list. So the short URLs you create are safe short URLs. Now, let us get onto the basics of what web services are and how to consume them in our web applications.
Background
A little about web services first. A Web service is a method of communication between two electronic devices over a network. Programmers implement web services for a variety of purposes such as to provide a middleware to access a remote database, to provide a middleware to access an application software over the web, etc. Web services basically have a number of procedures and functions that a remote user can call in order to access the service. So a web service for a basic calculator will have different remote procedures such as Add(number1,number2)
, Subtract(number1,number2)
, etc. that a user can call in his application if he wants a calculator type functionality in his application.
Now, how to call a web service. Web services can be called in our web applications using simple HttpWebRequest
and HttpWebResponse
methods. These are the two methods which are used to send information (request for data) over the web and get a response (fetch data) via HTTP. For actual implementation in different languages, read on.
Using the Code
Tinyit.cc
provides an easy to use web API through which a user can create short links and get number of hits for a short link. We will implement the API in 3 languages - C#, VB and PHP.
The web URL to access the Tinyit.cc
's link shortening API is:
http://tinyit.cc/api.php?url=LONG_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY"
Here, LONG_URL_WITHOUT_HTTP
is the long URL that needs to be shortened and without the 'http://
' header, USERNAME
is your Tinyit.cc
username and APIKEY
is your Tinyit.cc
API key. Get a new API key from here.
Here is the simple C#.NET code to create short URL using Tinyit.cc
API.
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);
}
So, using simple HttpWebRequest
and HttpWebResponse
methods, we have implemented the Tinyit.cc
link shortening API in our application.
Here is the simple VB.NET code to create short URL using Tinyit.cc
API.
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
Here is the simple PHP code to create short URL using Tinyit.cc
API.
$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
Fetching Number of Hits for the Short URL
The web URL to fetch the number of hits for the short link is:
http://tinyit.cc/api.php?url=SHORT_URL_WITHOUT_HTTP&user=USERNAME&api=APIKEY&getclicks=1"
Just note the new switch we have appended to the API request URL - 'getclicks=1
'. This switch signifies that we are interested in fetching the number of hits for our short link rather than creating a new short link. Pretty simple. So the code remains almost the same except that we need to change the API access URL.
Here is the simple C#.NET code to fetch the number of hits for our short URL:
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);
}
So, using simple HttpWebRequest
and HttpWebResponse
methods, we have implemented the Tinyit.cc
link shortening API in our application.
Here is the simple VB.NET code to fetch the number of hits for our short URL:
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
Here is the simple PHP code to fetch the number of hits for our short URL:
$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
Pretty simple and quick. All the short links you create via API will be available in your Tinyit.cc
user control panel. You can access the link statistics from here.
History
- 4th April, 2011: Initial post
Post Comment
8wiBaa This very blog is without a doubt awesome as well as informative. I have found helluva helpful tips out of it. I ad love to return over and over again. Cheers!
ÿþ<> カセット式デイジーホイール豊富な書体が揃った、美しいタイプフェースを印字するデイジーホイール。ブラザー独自のカセット式だから、ワンタッチで交換が可能です持ち運び可能な4.5kgの軽量設計持ち運びが楽で、使わない時にも気軽にしまっておける軽量ボディ。文書作成の場所を選ばないポータブルなタイプライターですカナ英文コンビのキー配列PCと同様なカナ英文コンビのキー配列を採用。
ベクトル・サイズガイドはこちら※こちらの商品については下記店舗までお問い合わせ下さい。ベクトル中庄店TEL 086 463 6363この商品と同じブランドの商品を見る【INDIVIインディヴィ】 アンゴラタートルニットセーター 38/E9 レディース 【ベクトル 古着】【中古】 140422ブランドINDIVIインディヴィ表記サイズ38実寸サイズ肩幅:29cm 身幅:39cm 着丈:53cm 袖丈: cm 素材アンゴラ80 ナイロン20色パープル仕様 状態若干の使用感や、生地の毛羽立ちはございますが、その他は特に目立った汚れやダメージも無くまだまだ着用していただけると思います。付属品 備考 【INDIVIインディヴィ】 アンゴラタートルニットセーター 38/E9 レディース 【ベクトル 古着】【中古】 140422状態ランク ”B”詳細については、下記および「状態ランクについて」ページをご覧ください。
ÿþ<> いくら走っても追いつかない水溜りが、「逃げ水」だと言うのを、おじさんに教えられて初めて知った市内には散水車用の給水ポイントがいくつかある。そのつである春光町のポイントに着いた。タンクへの給水が終わるまで時間があったので、すぐそばにある市場の中をぶらぶらしていたそこには何軒かの店が入っていて、当時、生活できる「衣・食」ほとんどの品物が並んでいた。
その思いから、今でもコツコツ勉強していますし、専門外の勉強会にも積極的に参加するようにしていますその一方で、「自分は何でもできる」と過信してはいけないと思っています。医者にとって大切なのは、「自分にできるのは、ここまで」という範囲を認識すること。なかには、できないことをやろうとして、ひどい治療をする人もいます。