我正在創建httpClient,並且當我發送httpPost方法時,如何將一個主體附加到httpRequest?httpPost方法
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();
}
例如,字符串是
「< HTML> <頭>標題< /報頭> <體>我體</BODY>」
哪裏該字符串附加到所述請求消息?
謝謝:)
setEntity方法僅需要實體...我想知道如何將字符串解析爲實體.. – Ken