java——HttpClient 代理模式发送Http Https
在setProxy()方法中设置代理IP后可以将url中的域名换成这个代理IP。
http很简单,但是https这样会报错。
问题:如何使用代理发送https请求?
客户端发送https请求之前会先向这台服务器请求ssl证书,并在客服端对这个证书做一个校验。
而使用代理IP时,实际上请求打到了这个代理IP上,而客户端并不知道这件事,他仍然在等待url域名中所对应的ssl证书,而这代理ip对应的服务器实际上并没有这个证书,导致了https请求失败。
解决方法:
HttpClient中有一个 类,里面的方法中包含了需要验证的所有ssl证书类型,而我们只需要重写这个方法,并把需要验证的证书设置为空,即命令客户端不验证任何ssl证书,就ok了。
关于http请求头中的host字段:
https://blog.csdn.net/yzpbright/article/details/51052008
代码:
private JSONObject HttpPing(String dns, String uri, String qlb, String scheme) { String url = scheme + "://" + dns + uri; CloseableHttpClient httpclient = null; if (scheme.equals("http")) { httpclient = getHttpClient(qlb); } else if (scheme.equals("https")) { httpclient = getHttpsClient(qlb); } else{ return null; } HttpGet httpGet = new HttpGet(url); CloseableHttpResponse httpResp = null; try { httpResp = httpclient.execute(httpGet); } catch (IOException e) { e.printStackTrace(); } try { int statusCode = httpResp.getStatusLine().getStatusCode(); if (statusCode == org.apache.http.HttpStatus.SC_OK) { return JSONObject.parseObject(EntityUtils.toString(httpResp.getEntity(), "UTF-8")); } } catch (Exception e) { e.printStackTrace(); } finally { try { httpResp.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } private CloseableHttpClient getHttpClient(String qlb) { HttpHost proxy = new HttpHost(qlb, 80, "http"); //把代理设置到请求配置 RequestConfig defaultRequestConfig = RequestConfig.custom() .setProxy(proxy) .build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); return httpclient; } private CloseableHttpClient getHttpsClient(String qlb) {
//这里设置客户端不检测服务器ssl证书 try { X509TrustManager x509mgr = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) { } public void checkServerTrusted(X509Certificate[] xcs, String string) { } public X509Certificate[] getAcceptedIssuers() { return null; } }; SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { x509mgr }, null); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpHost proxy = new HttpHost(qlb, 443, "https"); RequestConfig defaultRequestConfig = RequestConfig.custom() .setProxy(proxy) .build(); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(defaultRequestConfig).build(); return httpclient; } catch (Exception e) { e.printStackTrace(); } return null; }