java通过httpClient发送json格式数据请求(GET方式)

概述

在开发过程中和第三方系统对接时遇到需要使用GET请求传递JSON参数,现整理请求方式如下。
重写HttpGetWithEntity类
下面是最关键的代码,通过
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
    public final static String METHOD_NAME = "GET";

    public HttpGetWithEntity() {
        super();
    }

    public HttpGetWithEntity(final URI uri) {
        super();
        setURI(uri);
    }
    
    public HttpGetWithEntity(final String uri) {
        super();
        setURI(URI.create(uri));
    }

    @Override
    public String getMethod() {
    // TODO Auto-generated method stub
        return METHOD_NAME;
    }

}
调用方法
public class test{
    

    public static String processGetWithData(String url, String jsonParams) throws ServiceException {
    String result = null;
    org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
    httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
    try {
        HttpGetWithEntity e = new HttpGetWithEntity(url);
        e.setHeader("Content-Type", "application/json");
        if (!StringUtils.isEmpty(jsonParams)) {
            StringEntity se = new StringEntity(jsonParams);
            e.setEntity(se);
        }
    
        HttpResponse response = httpclient.execute(e);
        
        int STATUS_CODE = response.getStatusLine().getStatusCode();
    
        if (STATUS_CODE == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
            if (entity != null) {
                 entity.consumeContent();
            }
        } else {
            throw new ServiceException(HttpExceptionEnums.CODE_INTERNAL_EXCPTION);
        }
        httpclient.getConnectionManager().shutdown();
    } catch (Exception e) {
        throw new ServiceException(HttpExceptionEnums.Exception_EXCPTION);
    }
        return result;
    }
}
Java发送GET请求传递JSON数据,可以使用HttpClient库来实现。以下是一个示例代码: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGetWithEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import java.io.IOException; public class Main { public static void main(String\[\] args) { HttpClient httpClient = HttpClientBuilder.create().build(); String url = "http://example.com/api"; String json = "{\"key\":\"value\"}"; try { HttpGetWithEntity httpGet = new HttpGetWithEntity(url); httpGet.setEntity(new StringEntity(json)); HttpResponse response = httpClient.execute(httpGet); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们使用HttpClient库创建了一个HttpClient对象,并指定了请求的URL和JSON数据。然后,我们创建了一个HttpGetWithEntity对象,并将JSON数据设置为请求的实体。最后,我们执行GET请求并获取响应的内容。 请注意,这只是一个示例代码,你需要根据你的实际情况进行适当的修改。 #### 引用[.reference_title] - *1* [java http get post 和 发送json数据请求](https://blog.csdn.net/somdip/article/details/130584038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [java通过httpClient发送json格式数据请求(GET方式)](https://blog.csdn.net/xzj80927/article/details/127511924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜未央,流年殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值