HttpDelete与身体 [英] HttpDelete with body

查看:397
本文介绍了HttpDelete与身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用HttpDelete对象调用Web服务的删除方法。 Web服务的code解析JSON从消息的正文。不过,我没有理解如何将身体添加到HttpDelete对象。有没有办法做到这一点?

I'm attempting to use an HttpDelete object to invoke a web service's delete method. The web service's code parses JSON from the message's body. However, I'm failing to understand how to add a body to an HttpDelete object. Is there a way to do this?

使用HttpPut和HttpPost,我称之为setEntity方法并传递我的JSON。虽然目前没有出现任何这类方法HttpDelete

With HttpPut and HttpPost, I call the setEntity method and pass in my JSON. There doesn't appear to be any such method for HttpDelete.

如果没有办法设置一个机构的HttpDelete对象,你可以请联系我到使用超类HttpDelete,这样我可以设置方法(删除)的资源,并设置一个机构。我知道这是不理想的,但在这一点上我不能改变的Web服务。

If there is no way to set a body for an HttpDelete object, could you please link me to a resource that uses a super class of HttpDelete such that I can set the method (delete) and set a body. I know that isn't ideal, but at this point I can't alter the web service.

推荐答案

您是否尝试过覆盖 HttpEntityEnclosingRequestBase 如下:

Have you tried overriding HttpEntityEnclosingRequestBase as follows:

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;

@NotThreadSafe
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
    public static final String METHOD_NAME = "DELETE";
    public String getMethod() { return METHOD_NAME; }

    public HttpDeleteWithBody(final String uri) {
        super();
        setURI(URI.create(uri));
    }
    public HttpDeleteWithBody(final URI uri) {
        super();
        setURI(uri);
    }
    public HttpDeleteWithBody() { super(); }
}

这将创建一个 HttpDelete -lookalike,有一个 setEntity 方法。我认为抽象类做几乎所有的东西给你,所以这可能是所有需要。

That will create a HttpDelete-lookalike that has a setEntity method. I think the abstract class does almost everything for you, so that may be all that's needed.

FWIW,在code基于这来源HttpPost,谷歌打开了

FWIW, the code is based on this source to HttpPost that Google turned up.

这篇关于HttpDelete与身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆