Dec 04
[Tutorial] Accessing the TinyURL “API” from Java
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.
Similar Posts:
- [Tutorial] URL Shortening in Java using bit.ly
- [Tutorial] Amazon SOAP Product Advertising API from Java – Including Signing of Requests with WS-Security
- 5 Minute Guide to Spring and JMX
- 5 Minute Guide to the Java Amazon Associates Web Service API
- Quick Tip: JDBC ParameterizedSingleColumnRowMapper in Spring 2.5.2+


January 30th, 2009 at 2:31 pm
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?
February 4th, 2009 at 2:44 pm
@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
May 8th, 2009 at 10:00 am
[...] » [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: [...]
June 30th, 2009 at 3:19 am
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:
December 1st, 2009 at 12:19 am
[...] 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 [...]