问题
I want to perform a HTTP DELETE request with a xml in the body (not a form). How can I perform this HTTP DELETE request?
I tried with HttpURLConnection but there are some limitations with it.
I tried with the apache's HttpClient too. But I don't know how to send the xml without the structure of application/x-www-form-urlencoded
.
回答1:
This code will make it:
try {
HttpEntity entity = new StringEntity(jsonArray.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpDeleteWithBody httpDeleteWithBody = new HttpDeleteWithBody("http://10.17.1.72:8080/contacts");
httpDeleteWithBody.setEntity(entity);
HttpResponse response = httpClient.execute(httpDeleteWithBody);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
To access the response you can simply do: response.getStatusLine();
And here is the HttpDeleteWithBody class definition: HttpDeleteWithBody
回答2:
why not do this with HttpClient
class MyDelete extends HttpPost{
public MyDelete(String url){
super(url);
}
@Override
public String getMethod() {
return "DELETE";
}
}
it works well.
来源:https://stackoverflow.com/questions/15097104/how-can-i-perform-a-http-delete-request-with-body