Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode space as %20 in UrlEncodedFormEntity while executing apache HttpPost?

The web serive i am hitting requires the parameters as URLEncodedFormEntity. I am unable to change space to %20 as per requirement of the web service, instead space is converted to +.

My code is :

HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
            HTTP.UTF_8);
post.setEntity(entity);
HttpResponse resp = client.execute(post);

where parameters is List<NameValuePair> parameters.

I read through many posts and all suggest manuall change space to %20 after emcoding. Here, how do i access the entity and change it manually? Any help will be appreciated.

like image 624
rDroid Avatar asked May 22 '26 19:05

rDroid


1 Answers

The UrlEncodedFormEntity is basically a StringEntity with a custom constructor, you don't actually have to use it in order to create a usuable entity.

String entityValue = URLEncodedUtils.format(parameters, HTTP.UTF_8);
// Do your replacement here in entityValue
StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
// And now do your posting of this entity
like image 166
Jens Avatar answered May 24 '26 07:05

Jens



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!