java请求http接口

maven包:

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.10</version>
        </dependency>

 完成代码文件下载地址:

链接:https://pan.baidu.com/s/1fdeID83_ma_Fe3iPiW411g?pwd=1234 
提取码:1234

    /**
     * Http协议Get请求
     *
     * @param url     url带参数
     * @param headers headers值
     * @return get结果
     */
    public static String httpGet(String url, Map<String, String> headers) {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpGet httpGet = new HttpGet(url);
            //设置header
            setHeaders(httpGet, headers);
            //发起请求
            response = httpClient.execute(httpGet);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                log.error("httpGet请求异常,错误码:{}", response.getStatusLine().getStatusCode());
            } else {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
                //关闭响应流
                EntityUtils.consume(entity);
//                httpGet.releaseConnection();
            }

        } catch (Exception e) {
            log.error("httpGet异常", e);
        } finally {
            close(response);
        }
        return result;
    }
    /**
     * Http协议Post json请求
     *
     * @param url     url
     * @param json    json字符串
     * @param headers headers值
     * @return post结果
     */
    public static String httpPostJson(String url, String json, Map<String, String> headers) {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpPost httpPost = new HttpPost(url);
            //设置header
            httpPost.setHeader("Content-Type", "application/json");
            setHeaders(httpPost, headers);
            //填充参数
            httpPost.setEntity(new StringEntity(json, "UTF-8"));// 解决中文乱码问题
            //发起请求
            response = httpClient.execute(httpPost);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                log.error("httpPostJson请求异常,错误码:{}", response.getStatusLine().getStatusCode());
            } else {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
                //关闭响应流
                EntityUtils.consume(entity);
            }
        } catch (Exception e) {
            log.error("httpPostJson异常", e);
        } finally {
            close(response);
        }
        return result;
    }
    /**
     * Http协议Post form请求
     *
     * @param url      url
     * @param paramMap <String, Object>
     * @param headers  headers值
     * @return post结果
     */
    public static String httpPostForm(String url, Map<String, Object> paramMap, Map<String, String> headers) {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpPost httpPost = new HttpPost(url);
            //设置header
            setHeaders(httpPost, headers);
            //填充参数
            if (paramMap != null) {
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
                    list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
                }
                // 构造from表单对象
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");

                // 把表单放到post里
                httpPost.setEntity(urlEncodedFormEntity);
            }
            //发起请求
            response = httpClient.execute(httpPost);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                log.error("httpPostForm请求异常,错误码:{}", response.getStatusLine().getStatusCode());
            } else {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
                //关闭响应流
                EntityUtils.consume(entity);
            }
        } catch (Exception e) {
            log.error("httpPostForm异常", e);
        } finally {
            close(response);
        }
        return result;
    }
    /**
     * Http协议Put请求
     *
     * @param url     url
     * @param json    json字符串
     * @param headers headers值
     * @return put结果
     */
    public static String httpPut(String url, String json, Map<String, String> headers) {
        CloseableHttpResponse response = null;
        String result = "";
        try {
            httpClient = createHttpClient();
            HttpPut httpPut = new HttpPut(url);
            //设置header
            httpPut.setHeader("Content-Type", "application/json");
            setHeaders(httpPut, headers);
            //填充参数
            httpPut.setEntity(new StringEntity(json, "UTF-8"));// 解决中文乱码问题
            //发起请求
            response = httpClient.execute(httpPut);
            //处理返回结果
            if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() > 300) {
                log.error("httpPut请求异常,错误码:{}", response.getStatusLine().getStatusCode());
            } else {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
                //关闭响应流
                EntityUtils.consume(entity);
            }
        } catch (Exception e) {
            log.error("httpPut异常", e);
        } finally {
            close(response);
        }
        return result;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

渣娃程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值