Monday, July 25, 2011

How To: Expand a shortened URL in Java

Here's something I want to do more often in the future: Code snippets extracted from what I'm working on at the moment. In this case, it's JOPPFM.

You can find the snippet here. Cool, eh? Unfortunately it's not enough to tell Java to follow redirects. No, it's not even enough to open the connection afterwards. In order to get the unshortened URL you have to read something from the connection's stream. Reading the headers would work too. Simply read something. Anything.

Let's call this snippet JUrlExpander. ;)