java接受http请求接口带参

下面以http post的方式举例,我还写了一个小demo提供大家参考,里面有http get的方式。还有就是需要两个jar包httpclient-4.3.jar,httpcore-4.3.jar一定要注意版本。

demo下载地址

 /**
      * http post调用接口
      * @param url 请求地址  headerMap 请求头 paramMap请求参数
      * @return
      * @throws Exception
      */
public static String httpPost(String url, Map<String, String> headerMap, Map<String, String> paramMap) throws Exception {
    String result = null;
    CloseableHttpResponse response = null;
    ConnectionKeepAliveStrategy keepAliveStrategy = new DefaultConnectionKeepAliveStrategy() {
        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            long keepAlive = super.getKeepAliveDuration(response, context);
            if (keepAlive == -1) {
                // Keep connections alive 60 seconds if a keep-alive value
                // has not be explicitly set by the server
                keepAlive = 1000L * 60;
            }
            return keepAlive;
        }
    };
    
    CloseableHttpClient httpClient = HttpClients.custom().setKeepAliveStrategy(keepAliveStrategy).build();
    HttpPost httpPost = new HttpPost(url);
    try {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).setConnectionRequestTimeout(20000).build();
        httpPost.setConfig(requestConfig);
        // 设置HTTP头
        if(headerMap != null && headerMap.size() > 0) {
            for(String key : headerMap.keySet()) {
                httpPost.addHeader(key, headerMap.get(key));
            }
        }
        if(paramMap != null && paramMap.size() > 0) {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            for(String key : paramMap.keySet()) {
                params.add(new BasicNameValuePair(key, paramMap.get(key)));
            }
            // 设置参数
            if (params != null && params.size() > 0) {
                // 将请求参数进行UTF-8编码,以便传输中文
                httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
            }
        }
        response = httpClient.execute(httpPost, new BasicHttpContext());
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consumeQuietly(entity);
        } else {
            log.error("request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + url);
        }
    } catch (IOException e) {
        log.error("request url=" + url + ", exception, msg=" + e.getMessage(), e);
    } finally {
        httpPost.releaseConnection();
        try {
            httpClient.close();
        } catch (IOException e) {
            //log.error(e.getMessage(), e);
        }
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    return result;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值