Escaping ampersands in URLs for HttpClient requests
Asked Answered
M

3

5

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.

Myogenic answered 1/4, 2010 at 4:25 Comment(1)
Possible duplicate of Escaping & in a URLParenteral
G
15

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.

Gamekeeper answered 1/4, 2010 at 4:30 Comment(3)
Sorry, doesn't seem to work, I'm still getting the double-encoded %2526? All URLEncoder does is replace the & with a %26, which is the same as my original replace operation.Myogenic
OK I fixed my answer. You shouldn't use .getEscapedPathQuery() because the title is now escaped by URLEncoder.Gamekeeper
Sorry, my brain was mush when I wrote my first reply -- you're right, it's enough to encode just parameter and leave the rest untouched. Thanks!Myogenic
G
1

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

Graphitize answered 1/4, 2010 at 7:13 Comment(0)
S
0

Use the URLEncoder class.

Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.

Safranine answered 1/4, 2010 at 4:31 Comment(1)
Readers should be aware that encoding to the application/x-www-form-urlencoded is not exactly the same as URI encoding. Many of the escapes are the same, but not all of them.Lineal

© 2022 - 2024 — McMap. All rights reserved.