java 带有正文的 Apache HttpClient GET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12535016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:17:23  来源:igfitidea点击:

Apache HttpClient GET with body

javaapache-httpclient-4.x

提问by Scott Swank

I am trying to send an HTTP GET with a json object in its body. Is there a way to set the body of an HttpClientHttpGet? I am looking for the equivalent of HttpPost#setEntity.

我正在尝试发送一个包含 json 对象的 HTTP GET。有没有办法设置HttpClientHttpGet的主体?我正在寻找等效的 HttpPost#setEntity。

回答by torbinsky

From what I know, you can't do this with the default HttpGet class that comes with the Apache library. However, you can subclass the HttpEntityEnclosingRequestBaseentity and set the method to GET. I haven't tested this, but I think the following example might be what you're looking for:

据我所知,您无法使用 Apache 库附带的默认 HttpGet 类来执行此操作。但是,您可以继承 HttpEntityEnclosureRequestBase实体并将方法设置为 GET。我还没有测试过这个,但我认为下面的例子可能是你正在寻找的:

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
    public final static String METHOD_NAME = "GET";

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}

Edit:

编辑:

You could then do the following:

然后,您可以执行以下操作:

...
HttpGetWithEntity e = new HttpGetWithEntity();
...
e.setEntity(yourEntity);
...
response = httpclient.execute(e);

回答by icapurro

Using torbinsky's answer I created the above class. This lets me use the same methods for HttpPost.

使用托宾斯基的回答我创建了上面的类。这让我可以对 HttpPost 使用相同的方法。

import java.net.URI;

import org.apache.http.client.methods.HttpPost;

public class HttpGetWithEntity extends HttpPost {

    public final static String METHOD_NAME = "GET";

    public HttpGetWithEntity(URI url) {
        super(url);
    }

    public HttpGetWithEntity(String url) {
        super(url);
    }

    @Override
    public String getMethod() {
        return METHOD_NAME;
    }
}

回答by user2389517

How we can send request uri in this example just like HttpGet & HttpPost ???

在这个例子中,我们如何像 HttpGet 和 HttpPost 一样发送请求 uri ???

 public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase
 {
    public final static String METHOD_NAME = "GET";
    @Override
     public String getMethod() {
         return METHOD_NAME;
     } 

        HttpGetWithEntity e = new HttpGetWithEntity(); 
        e.setEntity(yourEntity); 
        response = httpclient.execute(e); 
}

回答by Brandon Lyday

In addition torbinsky's answer, you can add these constructors to the class to make it easier to set the uri:

除了 torbinsky 的回答之外,您还可以将这些构造函数添加到类中,以便更轻松地设置 uri:

public HttpGetWithEntity(String uri) throws URISyntaxException{
    this.setURI(new URI(uri));
}

public HttpGetWithEntity(URI uri){
    this.setURI(uri);
}

The setURImethod is inherited from HttpEntityEnclosingRequestBaseand can also be used outside the constructor.

setURI方法是从HttpEntityEnclosingRequestBase构造函数继承而来的,也可以在构造函数之外使用。