Dec 04

[Tutorial] Accessing the TinyURL “API” from Java

Tag: apache,httpclient,java,tinyurl,tutorial,web,webservicespmularien @ 10:13 pm

TinyURL is a service that has been around for a while, but recently regained popularity due to its widespread use on Twitter.

Recently, I poked around and wrote up a simple Java method to, given a URL (TinyURL supports only GET requests), generate a TinyURL from it in Java. This is really the only “API” supported by the TinyURL service, but it’s a handy one!

You’ll need Apache HttpClient 3.1 for this.

Without further ado, here’s the code:

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
 
public abstract class TinyURLUtils {
	public static String getTinyUrl(String fullUrl) throws HttpException, IOException {
		HttpClient httpclient = new HttpClient();
 
		// Prepare a request object
		HttpMethod method = new GetMethod("http://tinyurl.com/api-create.php"); 
		method.setQueryString(new NameValuePair[]{new NameValuePair("url",fullUrl)});
		httpclient.executeMethod(method);
		String tinyUrl = method.getResponseBodyAsString();
		method.releaseConnection();
		return tinyUrl;
	}
}

Then you’d call the method as follows:

String tinyUrl = TinyURLUtils.getTinyUrl("http://www.mularien.com/blog/");
System.out.println(tinyUrl); // --> http://tinyurl.com/5cporq

You’re welcome to use / improve this code in any way (obviously, I didn’t consider or care about proper exception handling), ideally linking to my blog as the source.

Enjoy!

Note that this makes an HTTP request directly, so this will require some modification if you’re making the call from behind a proxy server. If there’s a need, I can post a follow-up entry on how to set up a proxy server with Apache HTTP Client.

8 Responses to “[Tutorial] Accessing the TinyURL “API” from Java”

  1. DLackey says:

    I would be interested in how to build a tinyurl generate for use within a corporate lan and behind a firewall. Can you offer any insight or direction on how to do this?

  2. pmularien says:

    @DLackey
    For internal use? Or because you can’t hit the TinyURL site from behind the firewall?

    If you’re planning on rolling your own service, have a look at this answer on LinkedIn: Link

  3. Tweeting tiny URLs using Java « Scotsman on a Horse says:

    [...] » [Tutorial] Accessing the TinyURL “API” from Java – It’s Only Software Recently, I poked around and wrote up a simple Java method to, given a URL (TinyURL supports only GET requests), generate a TinyURL from it in Java. This is really the only “API” supported by the TinyURL service, but it’s a handy one! Published in: [...]

  4. gdoko says:

    Thanks Peter for taking the time to post this. In case someone has difficulties with making the sample code run; try to download: commons-httpclient-3.1.jar, commons-logging-1.1.1.jar, and commons-codec-1.3.jar. Additionally, in order to have the code working without any warnings (due to getResponseBodyAsStream) you can use the following slightly modified version:

    import java.io.InputStream;
    import java.io.IOException;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpException;
    import org.apache.commons.httpclient.HttpMethod;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;
     
    public abstract class TinyURL {
    	public static String getTinyUrl( String fullUrl ) throws HttpException, IOException {
    
    		HttpClient httpclient = new HttpClient();
    		HttpMethod method = new GetMethod( "http://tinyurl.com/api-create.php" ); 
    		method.setQueryString( new NameValuePair[] { new NameValuePair( "url", fullUrl ) } );
    		httpclient.executeMethod( method );
    
    		InputStream is = method.getResponseBodyAsStream();
    		StringBuffer sb = new StringBuffer();
    		for( int i = is.read(); i != -1; i = is.read() ) {
    			sb.append( (char) i );
    		}
    
    		String tinyUrl = sb.toString();
    		method.releaseConnection();
    		return tinyUrl;
    	}
    }
  5. » [Tutorial] URL Shortening in Java using bit.ly – It’s Only Software says:

    [...] while ago, I had written up a tutorial on accessing the TinyURL API from Java. I was recently playing with the bit.ly API and decided to write up a quick tutorial on generating [...]

  6. dBune says:

    Thanks Peter for sharing the code, and thanks gdoko for the improvements, yes the warnings are gone with the corrected code.

  7. java tutorial says:

    I was searching for such a site to learn
    Hibernate, Spring..applications
    now i got
    it interesting to know that servlets are used
    here..
    thanks for sharing.

  8. sivaraj says:

    Hi,
    I have account in bitly. I have customized twitter implementation with the help of bitly url shorter. Thanks thats great..

    Aim : I have to short the url with the help of bitly API. Current implementation is i have sent my url as query string and that process happen beyond my firewall. Is is possible to short the url with the help of your API with in my network.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>