zoukankan      html  css  js  c++  java
  • Http && Https(绕过证书) 请求工具类 (Java)

    public class HttpRequestUtil {
    
        private static final String HTTP = "http";
        private static final String HTTPS = "https";
        private static SSLConnectionSocketFactory sslsf = null;
        private static PoolingHttpClientConnectionManager cm = null;
        private static SSLContextBuilder builder = null;
        static {
            try {
                builder = new SSLContextBuilder();
                // 全部信任 不做身份鉴定
                builder.loadTrustMaterial(null, new TrustStrategy() {
                    @Override
                    public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                        return true;
                    }
                });
                sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
                Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                        .register(HTTP, new PlainConnectionSocketFactory())
                        .register(HTTPS, sslsf)
                        .build();
                cm = new PoolingHttpClientConnectionManager(registry);
                cm.setMaxTotal(200);//max connection 设置最大连接数
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /**
         * httpClient post请求
         * @param url 请求url
         * @param params 请求参数 form提交适用
         * @param encoding 请求实体 json/xml提交适用
         * @return 可能为空 需要处理
         * @throws Exception
         *
         */
        public static String post(String  url, String params, String encoding) throws Exception {
            String result = "";
            CloseableHttpClient httpClient = null;
            try {
                httpClient = getHttpClient();
                HttpPost httpPost = new HttpPost(url);
                // 设置头信息
                httpPost.addHeader("Content-Type", "application/json;charset=" + encoding);
                // 消息实体
                httpPost.setEntity(new StringEntity(params, encoding));
                //发起请求
                HttpResponse httpResponse = httpClient.execute(httpPost);
                //解析返回消息实体
                HttpEntity resEntity = httpResponse.getEntity();
                if (resEntity != null) {
                    result= EntityUtils.toString(resEntity);
                    log.info("result==="+result);
                }
            } catch (Exception e) {
                throw e;
            } finally {
                if (httpClient != null) {
                    httpClient.close();
                }
            }
            return result;
        }
        public static CloseableHttpClient getHttpClient() throws Exception {
            CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .setConnectionManager(cm)
                    .setConnectionManagerShared(true)
                    .build();
            return httpClient;
        }
    }
  • 相关阅读:
    Sql Server textcopy 的使用
    看来,我还是不适合犯罪
    Oracle 游标范例
    嵌入式学习方法 关于ARM+linux(转)
    存储控制器与外设之间的关系 (SDRAM与BANK6连接概述)
    socket和多线程可移植性编程基础(转)
    Linux设备驱动开发环境的搭建(转)
    Android JNI知识简介
    Linux USB驱动框架分析
    LINUX系统中动态链接库的创建和使用
  • 原文地址:https://www.cnblogs.com/yang1314/p/10515368.html
Copyright © 2011-2022 走看看