简体   繁体   English

如何使用body执行HTTP DELETE请求?

[英]How can I perform a HTTP DELETE request with body?

I want to perform a HTTP DELETE request with a xml in the body (not a form). 我想在正文中使用xml执行HTTP DELETE请求(而不是表单)。 How can I perform this HTTP DELETE request? 如何执行此HTTP DELETE请求?

I tried with HttpURLConnection but there are some limitations with it . 我试过HttpURLConnection它有一些限制

I tried with the apache's HttpClient too. 我也试过了apache的HttpClient But I don't know how to send the xml without the structure of application/x-www-form-urlencoded . 但是我不知道如何在没有application/x-www-form-urlencoded的结构的情况下发送xml。

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(); 要访问响应,您只需执行以下操作: response.getStatusLine();

And here is the HttpDeleteWithBody class definition: HttpDeleteWithBody 这里是HttpDeleteWithBody类定义: HttpDeleteWithBody

why not do this with HttpClient 为什么不用HttpClient来做这件事

class MyDelete extends HttpPost{
    public MyDelete(String url){
        super(url);
    }
    @Override
    public String getMethod() {
        return "DELETE";
    }

}

it works well. 它运作良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM