http get Post请求工具类

测试成功,可以运行。

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;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/hqm12345qw/article/details/79806843