Question

When I try to shorten a link with "#,&" character I get an exception. Is there a way to handle these character properly?

This is a sample code that works:

String shortUrl = bitly.getShortUrl("http://z"); //Works

If I add for example '&' or '%25' to the string it will throw an exception:

String shortUrl = bitly.getShortUrl("http://z%26"); // Exception 
String shortUrl = bitly.getShortUrl("http://z&"); // Exception

The getShortUrl function from this Java class.

Thanks

Was it helpful?

Solution

That library (the Java class you link to) doesn't escape the URL... that's pretty awful.

Excerpt:

private String getBitlyHttpResponseText(String urlToShorten) throws IOException {
  String uri = getBitlyUrl() + urlToShorten + bitlyAuth;
  HttpGet httpGet = new HttpGet(uri);
  ...

Notice how urlToShorten isn't escaped in any way, shape or form. Prone to injection-style attacks, and just generally doesn't work.

Anyway, you'll need to escape urlToShorten.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow