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.



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 [...]
August 25th, 2010 at 10:56 am
Thanks Peter for sharing the code, and thanks gdoko for the improvements, yes the warnings are gone with the corrected code.
October 26th, 2010 at 10:55 am
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.
August 22nd, 2011 at 8:23 am
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.