Apache HttpClient GET与body [英] Apache HttpClient GET with body

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

问题描述

我正在尝试在其正文中发送带有json对象的HTTP GET。有没有办法设置 HttpClient HttpGet的正文?我正在寻找等效的HttpPost#setEntity。

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#setEntity.

推荐答案

据我所知,你不能用默认的方式做到这一点Apache库附带的HttpGet类。但是,您可以继承 HttpEntityEnclosingRequestBase 实体并将方法设置为GET。我没有对此进行测试,但我认为以下示例可能是您正在寻找的内容:

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

编辑:

然后你可以执行以下操作:

You could then do the following:

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

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

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