Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding parameter to HttpPost on Apache's httpclient

I am trying to set some Http parameters in the HttpPost object.

HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);

It looks like the parameter is not set at all. Do you have any idea why this is happening?

Thank you

like image 617
pokeRex110 Avatar asked Feb 20 '12 14:02

pokeRex110


2 Answers

For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :

StringBuilder requestUrl = new StringBuilder("your_url");

String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);

HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());

NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.

like image 182
Cyril N. Avatar answered Oct 19 '22 13:10

Cyril N.


HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);
like image 28
joker Avatar answered Oct 19 '22 13:10

joker