http get Post请求工具类

1 篇文章 0 订阅
1 篇文章 0 订阅

测试成功,可以运行。

public class CreateHttpClientUtil {
        /***
         * httpClient post
         * @param url  路径
         * @param list   List list类型的参数
         * @return
         * @throws Exception
         */
        public static JSONObject createHttpClientDoPost(String url, List list) throws Exception {
            JSONObject jsonResult = null;
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
            httpPost.setHeader("Accept", "application/json");
            httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpPost);
                StatusLine status = response.getStatusLine();
                int state = status.getStatusCode();
                if (state == HttpStatus.SC_OK) {
                    HttpEntity responseEntity = response.getEntity();
                    String jsonString = EntityUtils.toString(responseEntity);
                    jsonResult = JSONObject.parseObject(jsonString);
                    logger.info("post请求成功");
                    return jsonResult;
                } else {
                    logger.error("请求失败,返回"+state+ "(" + url + ")");
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("消息处理异常");
            } finally {
                if (null != response) {
                    try {
                        response.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        logger.error("关闭失败");
                    }
                }
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    logger.error("关闭失败");
                }
            }
            return null;
        }

        /**
         * httpClient Get
         * @param url
         * @param access_token  参数 String
         * @throws HttpException
         * @throws IOException
         */
        public static JSONObject createHttpClientDoGet(String url,String access_token) throws HttpException, IOException{
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url + "?access_token=" + access_token);
            JSONObject jsonResult = null;
            //设置超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
            httpGet.setConfig(requestConfig);
            CloseableHttpResponse httpResponse = null;
            try {
                httpResponse = httpClient.execute(httpGet);
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity responseEntity = httpResponse.getEntity();
                    String jsonString = EntityUtils.toString(responseEntity, "utf-8");
                    jsonResult = JSONObject.parseObject(jsonString);
                    logger.info("http get请求发送成功");
                    return jsonResult;
                } else {
                    logger.error("get请求提交失败"+url);
                }
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("IO Exception");
            } finally {
                httpGet.releaseConnection();
            }
            return null;
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值