HttpDelete与身体身体、HttpDelete

2023-09-12 03:22:42 作者:一眼心动

我尝试使用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.