• HttpClient使用笔记


    依赖

       <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.13</version>
            </dependency>
    

    工具类

    public class HttpUtil {
    
        private static CloseableHttpClient httpClient;
    
        static {
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
            cm.setMaxTotal(100);
            cm.setDefaultMaxPerRoute(20);
            cm.setDefaultMaxPerRoute(50);
            httpClient = HttpClients.custom().setConnectionManager(cm).build();
        }
    
        public static String get(String url) {
            CloseableHttpResponse response = null;
            BufferedReader in = null;
            String result = "";
            try {
                HttpGet httpGet = new HttpGet(url);
                RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
                httpGet.setConfig(requestConfig);
                httpGet.setConfig(requestConfig);
                httpGet.addHeader("Content-type", "application/json; charset=utf-8");
                httpGet.setHeader("Accept", "application/json");
                response = httpClient.execute(httpGet);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                result = sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != response) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    
        public static String post(String url, String jsonString) {
            CloseableHttpResponse response = null;
            BufferedReader in = null;
            String result = "";
            try {
                HttpPost httpPost = new HttpPost(url);
                RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
                httpPost.setConfig(requestConfig);
                httpPost.setConfig(requestConfig);
                httpPost.addHeader("Content-type", "application/json; charset=utf-8");
                httpPost.setHeader("Accept", "application/json");
                httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
                response = httpClient.execute(httpPost);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
                result = sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != response) {
                        response.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    
    
    }
    

    使用

    //获取微信的AccessToken    
    private JSONObject invokeWxAccessToken(){
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(wxAccessConfig.getAccessTokenUrl());
            stringBuffer.append(WechatConstant.APP_ID).append(wxAccessConfig.getAppId());
            stringBuffer.append(WechatConstant.APP_SECRET).append(wxAccessConfig.getDecryptAppSecret());
            stringBuffer.append(wxAccessConfig.getGrandType());
            //获取access_token
            String url = stringBuffer.toString();
            String response = HttpUtil.get(url);
            JSONObject resultJson = JSON.parseObject(response);
            if (resultJson.containsKey(WechatConstant.API_ERROR_CODE) && WX_SUCCESS_CODE != resultJson.getInteger(WechatConstant.API_ERROR_CODE)) {
                throw new BusinessException(String.valueOf(resultJson.get(WechatConstant.API_ERROR_MSG)), String.valueOf(resultJson.get(WechatConstant.API_ERROR_CODE)));
            }
            return resultJson;
        }
    
  • 相关阅读:
    ansible源码解读
    python标准模块(下)
    python学习之算法、自定义模块、系统标准模块(上)
    pathon 基础学习-集合(set),单双队列,深浅copy,内置函数
    python的map,filter,reduce学习
    python 列表
    python生成器、装饰器、正则
    python 模块学习
    python基础学习(一)--数据类型
    时间复杂度的计算
  • 原文地址:https://www.cnblogs.com/castamere/p/16069173.html
Copyright © 2020-2023  润新知