Consuming URL Shortening Services - Tweetburner (Twurl)

No.of Views776
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  31 Aug 2010 12:08:19
Tag : Web Service , Miscellaneous
Just another article of the URL shortening services series. Today, we are going to talk about another hot and easy-to-use service, it's Tweetburner. If you haven't used it before, then it's the time to. We're going to discuss how to use Tweetburner first. After that, we'll inspect its API and learn how to use it in your .NET application.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 
This article is also available in my blog, Just Like a Magic.
هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.
Read more about URL shortening services here.

Contents Contents of this article:

  • Contents
  • Overview
  • Introduction
  • Description
  • API
  • Where to go next

Overview

Just another article of the URL shortening services series.

Today, we are going to talk about another hot and easy-to-use service, it's Tweetburner. If you haven't used it before, then it's the time to.

We're going to discuss how to use Tweetburner first. After that, we'll inspect its API and learn how to use it in your .NET application.

Introduction

Again, one of the most popular URL shortening services ever.

Today is dedicated for Tweetburner (known as twurl,) one of the hot, simple, and easy-to-use shortening services that you can compare to is.gd.

Description

When you visit Tweetburner website (via http://tweetburner.com or http://twurl.nl,) you can see that it allows users to register to gain more functionalities (specifically, link analytics.) However, at the time of this writing, the account page is disabled for technical issues and nothing interesting would happen if you register there.

One of the hot features of Tweetburner is that it allows you to post your links to twitter (you guessed) and friendfeed as soon as they're shortened just click 'Share this link' before you leave the page.

 

Unfortunately, you can't benefit from this sharing feature programmatically, but of course, you can create your own routines.

After shrinking your URL, you get a new short link about 22 characters long (18 in is.gd) prefixed with http://twurl.nl.

API

Actually, Tweetburner doesn't help you with an API. Instead, it provides you with a simple web page (used for shortening URLs) that you can access it from your code and get your short URLs.

Let's try it! Browse to our key page, http://tweetburner.com/links, and push your long URL and click the shortening button.

 

So how you can access this page via your .NET application and fill in its single field? Let's get the idea! If you check the API documentation page, you might find that you are required just to request information from that page, post it the required URL via a simple string included in the request body, link[url]={0} (where {0} is the long URL, and just wait for the response that would contain the short URL of course if the function succeed.

Do you find that 'link[url]={0}' strange? Try this with me! Browse to our page, http://tweetburner.com/links, and save it as HTML in your PC (not required, just grab its HTML code.)

Sure we are interested on this magical text box, so scroll down to its definition that looks like this:

<input id="link_url" name="link[url]" size="30" type="text" />

Notice that the text box is given the name 'link[url]', that's why we push 'link[url]={0}' on the request body. Given that hot information, you can push any data to any web form, just get the information required.

Now, let's code! The next function browses to our page, http://tweetburner.com/links, pushes the long URL specified, and gets the short URL back from the server. (Remember to include the namespace System.Net for the code to work properly.)

// C#

string Short(string url)
{
    url = Uri.EscapeUriString(url);

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://tweetburner.com/links");
    req.Timeout = 5000;
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";

    byte[] buffer = System.Text.Encoding.UTF8.GetBytes("link[url]=" + url);
    req.ContentLength = buffer.Length;

    System.IO.Stream ios = req.GetRequestStream();
    ios.Write(buffer, 0, buffer.Length);

    try
    {

        using (System.IO.StreamReader reader =
            new System.IO.StreamReader(req.GetResponse().GetResponseStream()))
        {
            return reader.ReadLine();
        }
    }
    catch (WebException ex)
    {
        return ex.Message;
    }
}
' VB.NET

Function Shorten(ByVal url As String) As String
    url = Uri.EscapeUriString(url)

    Dim req As HttpWebRequest = _
        CType(WebRequest.Create("http://tweetburner.com/links"), HttpWebRequest)
    req.Timeout = 5000
    req.Method = "POST"
    req.ContentType = "application/x-www-form-urlencoded"

    Dim buffer() As Byte = _
        System.Text.Encoding.UTF8.GetBytes("link[url]=" + url)
    req.ContentLength = buffer.Length

    Dim ios As System.IO.Stream = req.GetRequestStream()
    ios.Write(buffer, 0, buffer.Length)

    Try
        Dim reader As System.IO.StreamReader = _
            New System.IO.StreamReader(req.GetResponse().GetResponseStream())

        Dim retValue As String = reader.ReadLine()

        reader.Close()

        Return retValue
    Catch ex As WebException
        Return ex.Message
    End Try
End Function

Notice that we have specified the POST method because it's required if you are going to change some data in the server. It's worth mentioning too that we have set the content type to application/x-www-form-urlencoded because it's required if you are going to push data to a web form (it's usually perfect for all web forms except file-uploads.)

In addition, we have included the input required in the request stream.

What's next

Some other articles about URL shortening services are available here.

 
Sign Up to vote for this article
 
About Author
 
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Location-Egypt
Joined date-30 Jul 2009
Home Page-http://WithDotNet.net
Blog Page-http://JustLikeAMagic.com
Independent software developer, trainer, and technical writer from Egypt born in 1991
 
 
Other popularSectionarticles
    Another article of our endless series that talks about accessing URL shortening services programmatically.This article is talking about 1click.at shortening service, how you can use it, and how to access it via your C#/VB.NET application.
    Published Date : 27/Dec/2010
    This is another article of our URL shortening services series. This article is talking about X.co shortening service provided by Go Daddy. If you don’t know how to access this service from your .NET application, then it’s the time to. We’ll have a complete discussion of the WCF services offered by X.co. Then, we’ll consider the RESTful interfaces provided.
    Published Date : 23/Sep/2010
    This is a very hot article that you can’t leave without checking it first. This article is talking about the most popular and powerful URL shortening service ever, bit.ly. Today, we are going to talk about bit.ly API, its functions, and how you can access them from your .NET application. Don’t forget to download the sample code at the end of the article. Let’s go!
    Published Date : 02/Sep/2010
    This is another article that talks about URL shortening services. Today we are going to talk about Cligs, one of the popular shortening services on the web. Be prepared!
    Published Date : 02/Sep/2010
    Another article of our series that talks about accessing URL shortening services programmatically. This article is talking about is.gd shortening service, how you can use it, and how to access it via your C#/VB.NET application.
    Published Date : 30/Aug/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top