Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpGet url encoding problem (plus sign, +)

I'm sending a GET request with HttpClient but the + is not encoded.


1. If I pass the query parameter string unencoded like this

URI uri = new URI(scheme, host, path, query, null);
HttpGet get = new HttpGet(uri);

Then the + sign is not encoded and it is received as a space on the server. The rest of the url is encoded fine.

2.If I encode the parameters in the query string like this

param = URLEncoder.encode(param,"UTF-8");

Then I get a bunch of weird symbols on the server, probably because the url has been encoded twice.

3.If I only replace the + with %B2 like this

query = query.replaceAll("\\+","%B2");

Then %B2 is encoded when the GET is executed by HttpClient


How can I properly encode Get parameters with Apache HttpClient and make sure the + is encoded as well?

like image 398
siamii Avatar asked Apr 25 '26 19:04

siamii


1 Answers

Ok, the solution was that instead of creating the URI like this

URI uri = new URI(scheme, host, path, query, null);

One should create it like this

URIUtils.createURI(scheme, host, -1, path, query, null);

The purpose of the URIUtils class is

A collection of utilities for URIs, to workaround bugs within the class

no comment........

like image 131
siamii Avatar answered Apr 27 '26 07:04

siamii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!