I am creating httpClient and when I send httpPost method, how can I attach a body to the httpRequest ??
public String httpPost(String URL, String BODY) {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
try {
response = httpclient.execute(httpPost); // get response from executing client
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
body.append(statusLine + "\n");
HttpEntity e = response.getEntity();
String entity = EntityUtils.toString(e);
body.append(entity);
} else {
body.append(statusLine + "\n");
// System.out.println(statusLine);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpPost.releaseConnection();
}
return body.toString();
}
For example, the string is
” < html > < header > Header < /header> < body> I am body < /body> “
Where do I attach that string to the request message ?
Thank you 🙂
You can create a
StringEntity, set it to theHttpPostobject, and set the correctContent-Type:And then send your POST request as usual.