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

时间:2013-02-26 18:56:39

标签: android rest http http-delete

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

我尝试使用HttpURLConnectionthere are some limitations with it

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

2 个答案:

答案 0 :(得分:3)

此代码将成为:

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

答案 1 :(得分:1)

为什么不用HttpClient执行此操作

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

}

效果很好。