Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping ampersands in URLs for HttpClient requests

So I've got some Java code that uses Jakarta HttpClient like this:

URI aURI = new URI( "http://host/index.php?title=" + title + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery());

The problem is that if title includes any ampersands (&), they're considered parameter delimiters and the request goes screwy... and if I replace them with the URL-escaped equivalent %26, then this gets double-escaped by getEscapedPathQuery() into %2526.

I'm currently working around this by basically repairing the damage afterward:

URI aURI = new URI( "http://host/index.php?title=" + title.replace("&", "%26") + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery().replace("%2526", "%26"));

But there has to be a nicer way to do this, right? Note that the title can contain any number of unpredictable UTF-8 chars etc, so escaping everything else is a requirement.

like image 887
lambshaanxy Avatar asked Apr 01 '10 04:04

lambshaanxy


2 Answers

Here you go:

import java.net.URLEncoder;
...
...
URI aURI = new URI( "http://host/index.php?title=" + URLEncoder.encode(title,"UTF-8") + "&action=edit" );
GetMethod aRequest = new GetMethod( aURI.getPathQuery());

Check java.net.URLEncoder for more info.

like image 57
Strelok Avatar answered Oct 06 '22 00:10

Strelok


Why are you calling getEscapedPathQuery() if you don't want the escaping? Just decide who's responsibility it is and be consistent.

like image 20
Julian Reschke Avatar answered Oct 05 '22 23:10

Julian Reschke