How can I perform a HTTP DELETE request with body?

萝らか妹 提交于 2019-12-11 06:42:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!