Apache HttpClient GET with body

后端 未结 4 1574
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 23:45

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 HttpClient HttpGet? I am looking for the equivalent of HttpPost#setEntit

4条回答
  •  借酒劲吻你
    2020-12-06 00:17

    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 HttpEntityEnclosingRequestBase entity and set the method to GET. I haven't tested this, but I think the following example might be what you're looking for:

    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);
    

提交回复
热议问题