【问题标题】:How can I perform a HTTP DELETE request with body?如何使用正文执行 HTTP DELETE 请求?
【发布时间】:2013-02-26 18:56:39
【问题描述】:

我想在正文(不是表单)中使用 xml 执行 HTTP DELETE 请求。如何执行这个 HTTP DELETE 请求?

我尝试使用HttpURLConnectionthere are some limitations with it

我也尝试过使用 apache 的 HttpClient。但是我不知道如何发送没有application/x-www-form-urlencoded结构的xml。

【问题讨论】:

  • 在基于 REST 的 api 中支持 http 请求体。但我没有发现它在任何地方实现了删除请求体。嗯,很有趣
  • HttpDelete with body的可能重复
  • @WebnetMobile,这不是重复的。如果您想发送 application/x-www-form-urlencoded 结构但不适用于原始数据(如 xml),HttpDelete with body 可以解决问题。
  • 这能回答你的问题吗? HttpDelete with body

标签: android rest http http-delete


【解决方案1】:

这段代码可以实现:

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();
    }

要访问响应,您只需执行以下操作:response.getStatusLine();

这里是 HttpDeleteWithBody 类定义:HttpDeleteWithBody

【讨论】:

    【解决方案2】:

    为什么不用 HttpClient 这样做

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

    效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多