Https验证通过

/**
 * 发起get请求,并支持自定义Header
 *
 * @param url    请求RUL
 * @param header 报文头
 * @param params 请求属性
 * @return
 * @author jslixiaohong
 */
public static String get(String url, Map<String, String> header, Map<String, Object> params, int timeout) {
    SSLContextBuilder builder = null;
    SSLConnectionSocketFactory sslsf = null;

    CloseableHttpClient httpClient = null;
    // 超时设置
    RequestConfig requestConfig = getRequestConfig (timeout);
    CloseableHttpResponse response = null;
    try {
        //设置本客户端为受信任的客户端
        builder = new SSLContextBuilder ();
        builder.loadTrustMaterial (null, new TrustSelfSignedStrategy ());
        sslsf = new SSLConnectionSocketFactory (builder.build ());

        httpClient = HttpClients.custom ().setSSLSocketFactory (sslsf).build ();

        String requestParams = parseParams (params);
        if (!url.contains ("?")) {
            url += "?";
        }
        String extraUrl = url + (StringUtils.isEmpty (requestParams) ? "" : requestParams);
        HttpGet get = new HttpGet (extraUrl);
        if (!CollectionUtils.isEmpty (header)) {
            Set<String> keys = header.keySet ();
            Iterator<String> it = keys.iterator ();
            while (it.hasNext ()) {
                String headerKey = it.next ();
                get.setHeader (headerKey, header.get (headerKey));
            }
        }
        get.setConfig (requestConfig);
        response = httpClient.execute (get);
        return handleResponse (url, response);
    } catch (Exception e) {
        LOGGER.error ("访问链接异常[GET] url=" + url, e);
        return null;
    } finally {
        if (response != null) {
            try {
                response.close ();
            } catch (IOException e) {
                //ignore
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/jakeswang/article/details/79901038