1

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

我试过了,HttpURLConnection它有一些限制

我也尝试过使用apache HttpClient。但是我不知道如何在没有application/x-www-form-urlencoded.

4

2 回答 2

4

这段代码将使它:

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

于 2014-04-10T10:06:17.707 回答
3

为什么不使用 HttpClient 执行此操作

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

}

它运作良好。

于 2013-06-06T09:31:08.940 回答