由于 HttpGet 并没有像HttpPost 一样的setEntity方法,在实际应用中,可能有“发送带有body(json)的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;
}
}
然后使用
...
HttpGetWithEntity e = new HttpGetWithEntity();
...
e.setEntity(yourEntity);
...
response = httpclient.execute(e);
该博客介绍了一种解决HttpGet无法设置Entity的问题,通过创建一个名为HttpGetWithEntity的自定义类,扩展了HttpEntityEnclosingRequestBase,使得在GET请求中可以携带JSON数据。示例代码展示了如何构造并执行这样的请求。
1261

被折叠的 条评论
为什么被折叠?



