Reputation: 3696
I am currently programming an application that needs to send an http Post request to a server. The request needs to contain particular parameters which in an equivalent GET request would be encoded in the url. However, the body of the http Request needs to be correctly formatted xml which the server will parse. So, as I understand it, the file needs to be added to the request like so:
httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("url...");
if (httpContent != null){
InputStreamEntity httpStream = new InputStreamEntity(httpContent,-1);
httpStream.setContentType("text/xml");
httpStream.setChunked(true);
post.setEntity(httpStream);
}
And parameters need to be added as a name-value pair like this:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("userid", "12312"));
nameValuePairs.add(new BasicNameValuePair("sessionid", "234"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
now, I am pretty sure that the httpPost can only have one entity set unless I am misunderstanding it. So, How can I set the parameters and still retain the xml as the http body?
Thanks in advance!
Upvotes: 2
Views: 1042
Reputation: 28687
Don't use the UrlEncodedFormEntity
- this is still a form entity, and won't give you the "GET request" equivalent that you're looking for.
Instead, just append your parameters on to the URL passed to the HttpPost
constructor:
url...?userid=12312&sessionid=234
Upvotes: 2