HttpUtils——助力高效网络通信

  • 使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可:
1、创建HttpClient对象。
2、创建请求方法的实例,并指定请求URL。如果需要发送GET请求,
创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3、如果需要发送请求参数,可调用HttpGet、HttpPost
共同的setParams(HttpParams params)方法来添加请求参数;
对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)
方法来设置请求参数。
4、调用HttpClient对象的execute(HttpUriRequest request)
发送请求,该方法返回一个HttpResponse。
5、调用HttpResponse的getAllHeaders()、getHeaders(String name)
等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,
该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
6、释放连接。无论执行方法是否成功,都必须释放连接
  • 引入依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
  • 工具类
@Slf4j
public class HttpUtils {

    public static String defaultEncoding = "utf-8";

    /**
     * 发送http post请求,并返回响应实体
     *
     * @param url 请求地址
     * @return url响应实体
     */
    public static String postRequest(String url) {
        return postRequest(url, null, null);
    }

    /**
     * <p>方法名: postRequest</p>
     * <p>描述: 发送httpPost请求</p>
     *
     * @param url
     * @param params
     * @return
     */
    public static String postRequest(String url, Map<String, Object> params) {
        return postRequest(url, null, params);
    }

    /**
     * 发送http post请求,并返回响应实体
     *
     * @param url     访问的url
     * @param headers 请求需要添加的请求头
     * @param params  请求参数
     * @return
     */
    public static String postRequest(String url, Map<String, String> headers,
                                     Map<String, Object> params) {
        String result = null;
        CloseableHttpClient httpClient = buildHttpClient();
        HttpPost httpPost = new HttpPost(url);
        if (null != headers && headers.size() > 0) {
            for (Entry<String, String> entry : headers.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                httpPost.addHeader(new BasicHeader(key, value));
            }
        }

        if(isJSON(params.toString())){
            /**
             * json格式
             */
            StringEntity requestEntity = new StringEntity(params.toString(), "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);
        }else{
            /**
             * 名称值对节点类型
             */
            if (null != params && params.size() > 0) {
                List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
                    pairList.add(pair);
                }
                httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName(defaultEncoding)));
            }
        }


        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    log.info("【POST请求信息】,请求地址:{},请求参数:{}", url, params);
                    result = EntityUtils.toString(entity, Charset.forName(defaultEncoding));
                    log.info("【POST请求信息】,请求地址:{},请求参数:{},返回结果:{}", url, params,result);
                }
            } finally {
                response.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    /**
     * 发送http get请求
     *
     * @param url 请求url
     * @return url返回内容
     */
    public static String getRequest(String url) {
        return getRequest(url, null);
    }


    /**
     * 发送http get请求
     *
     * @param url    请求的url
     * @param params 请求的参数
     * @return
     */
    public static String getRequest(String url, Map<String, Object> params) {
        return getRequest(url, null, params);
    }

    /**
     * 发送http get请求
     *
     * @param url        请求的url
     * @param headersMap 请求头
     * @param params     请求的参数
     * @return
     */
    public static String getRequest(String url, Map<String, String> headersMap, Map<String, Object> params) {
        String result = null;
        CloseableHttpClient httpClient = buildHttpClient();
        try {
            String apiUrl = url;
            if (null != params && params.size() > 0) {
                StringBuffer param = new StringBuffer();
                int i = 0;
                for (String key : params.keySet()) {
                    if (i == 0)
                        param.append("?");
                    else
                        param.append("&");
                    param.append(key).append("=").append(params.get(key));
                    i++;
                }
                apiUrl += param;
            }

            HttpGet httpGet = new HttpGet(apiUrl);
            if (null != headersMap && headersMap.size() > 0) {
                for (Entry<String, String> entry : headersMap.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    httpGet.addHeader(new BasicHeader(key, value));
                }
            }
            CloseableHttpResponse response = httpClient.execute(httpGet);
            try {
                HttpEntity entity = response.getEntity();
                if (null != entity) {
                    log.info("【GET请求信息】,请求地址:{},请求参数:{}", url, params);
                    result = EntityUtils.toString(entity, defaultEncoding);
                    log.info("【GET请求信息】,请求地址:{},请求参数:{},返回结果:{}", url, params,result);
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 创建httpclient,支持http、https
     *
     * @return
     */
    public static CloseableHttpClient buildHttpClient() {
        try {
            RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder
                    .create();
            ConnectionSocketFactory factory = new PlainConnectionSocketFactory();
            builder.register("http", factory);
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            SSLContext context = SSLContexts.custom().useTLS()
                    .loadTrustMaterial(trustStore, new TrustStrategy() {
                        public boolean isTrusted(X509Certificate[] chain,
                                                 String authType) throws CertificateException {
                            return true;
                        }
                    }).build();
            LayeredConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(
                    context,
                    SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            builder.register("https", sslFactory);
            Registry<ConnectionSocketFactory> registry = builder.build();
            PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry);
            ConnectionConfig connConfig = ConnectionConfig.custom()
                    .setCharset(Charset.forName(defaultEncoding)).build();
            SocketConfig socketConfig = SocketConfig.custom()
                    .setSoTimeout(100000).build();
            manager.setDefaultConnectionConfig(connConfig);
            manager.setDefaultSocketConfig(socketConfig);
            return HttpClientBuilder.create().setConnectionManager(manager)
                    .build();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static boolean isJSON(String str) {
        boolean result = false;
        try {
            Object obj=JSON.parse(str);
            result = true;
        } catch (Exception e) {
            result=false;
        }
        return result;
    }

    public static void main(String[] args) {
        String url1 = "http://192.168.56.101:8196/test/get1";
        String result = HttpUtils.getRequest(url1);
        System.out.println(result);

        String url2 = "http://192.168.56.101:8196/test/get2";
        Map<String, Object> params = new HashMap<>();
        params.put("name", "一安未来1");
        params.put("addr", "北京1");
        String result2 = HttpUtils.postRequest(url2, null,params);
        System.out.println(result2);

        String url3 = "http://192.168.56.101:8196/test/get3";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "一安未来2");
        jsonObject.put("addr", "北京2");
        String result3 = HttpUtils.postRequest(url3, null,jsonObject);
        System.out.println(result3);
    }

【GET请求信息】,请求地址:http://192.168.56.101:8196/test/get1,请求参数:null
【GET请求信息】,请求地址:https://192.168.56.101:8196/test/get1,请求参数:null,返回结果:{"result":"success"}

【POST请求信息】,请求地址:http://192.168.56.101:8196/test/get2,请求参数:{name=一安未来1, addr=北京1}
【POST请求信息】,请求地址:http://192.168.56.101:8196/test/get2,请求参数:{name=一安未来1, addr=北京1},返回结果:{"name":"一安未来1","addr":"北京1"}

【POST请求信息】,请求地址:http://192.168.56.101:8196/test/get3,请求参数:{"name":"一安未来2","addr":"北京2"}
【POST请求信息】,请求地址:http://192.168.56.101:8196/test/get3,请求参数:{"name":"一安未来2","addr":"北京2"},返回结果:{"name":"一安未来2","addr":"北京2"}

}
  • 接口类
@RestController
public class TestController {


    @GetMapping("get1")
    public String test1(){
        return "{\"result\":\"success\"}";
    }


    @PostMapping("get2")
    private Map<String, Object> test2(@RequestParam Map<String,Object> map){
        return map;
    }

    @PostMapping("get3")
    private Map test3(@RequestBody Map<String,Object> map){
        return map;
    }

}