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() {
        return METHOD_NAME;
    }

}

工具类

    /**
     * get 发送 json 参数
     * @param ropPath
     * @param path
     * @param sk
     * @param ak
     * @param date
     * @param jsonStr
     * @return
     */
    public static String GetOpenUrlJsonString(String ropPath, String path, String sk, String ak, String date,String jsonStr) {
        BufferedReader in = null;
        String result = null;

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append( "GET\n" );
        stringBuilder.append( "application/json\n" );
        stringBuilder.append( date + "\n" );
        stringBuilder.append( path );
        String pathAndUrl = stringBuilder.toString();
        String Sig = HMACSHA1Utils.getHmacSHA1( sk, pathAndUrl, "HmacSHA1" );
        Base64.Encoder encoder = Base64.getEncoder();
        //编码
        String Signature = encoder.encodeToString( Sig.getBytes() );
        String Authorization = "ROP " + ak + ":" + Signature;

        try {
            org.apache.http.client.HttpClient httpclient  = new DefaultHttpClient();

            HttpGetWithEntity requestEntity = new HttpGetWithEntity(ropPath + path);
            requestEntity.setHeader("Content-Type", "application/json");
            requestEntity.setHeader( "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon;)" );
            requestEntity.setHeader( "Date", date );
            requestEntity.setHeader( "Authorization", Authorization );



            if (!StringUtils.isEmpty(jsonStr)) {
                StringEntity se = new StringEntity(jsonStr);
                requestEntity.setEntity(se);
            }

            HttpResponse response = httpclient.execute(requestEntity);

            int STATUS_CODE = response.getStatusLine().getStatusCode();

            if (STATUS_CODE == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity);
                    return result;
                }
                if (entity != null) {
                    entity.consumeContent();
                }
            } else {
                throw new ServiceException("调用失败");
            }
            httpclient.getConnectionManager().shutdown();
        } catch (Exception e) {

        }
        return null;

    }

调用:

String date = getGMTDate();
//3.7 获取资源的url
String getUrl = "/res/resources/url";
String jsonStr = "{\"resourceId\":\""+resourceId+"\"}";
String resultDetail = HttpClientUtils.GetOpenUrlJsonString( xxUrl, getUrl, AccessKeySecret, AccessKey, date,jsonStr );

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值