/** * 发起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 } } } }
Https验证通过
最新推荐文章于 2024-01-23 09:48:39 发布