All*_*uin 10 java apache-httpclient-4.x unirest
我正在使用Unirest(java版本)来发出GET和POST请求.但是我在访问SSL加密站点时遇到问题,因为我的程序在公司网络后面,网络管理员为我设置了防火墙映射.例如foobar.com
映射到56.1.89.12:4444
.但是当我向地址发出请求时,我将收到以下ssl证书错误:
com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLException: hostname in certificate didn't match: <56.1.89.12> != <www.foobar.com>
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:52)
Run Code Online (Sandbox Code Playgroud)
我看到Unirest
有使用自定义的高级配置httpclient
.所以我使用
Unirest.setHttpClient(MyHttpClient.makeClient());
HttpResponse<String> res = null;
try {
res = Unirest.get(urlstr).asString();
} catch (UnirestException e) {
e.printStackTrace();
}
String jsonstr = res.getBody();
Run Code Online (Sandbox Code Playgroud)
所述makeClient
的方法MyHttpClient
为:
public static HttpClient makeClient(){
SSLContextBuilder builder = new SSLContextBuilder();
CloseableHttpClient httpclient = null;
try {
// builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
builder.loadTrustMaterial(null, new TrustStrategy(){
public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
});
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
httpclient = HttpClients.custom().setSSLSocketFactory(
sslsf).build();
System.out.println("custom httpclient called");
System.out.println(httpclient);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return httpclient;
}
Run Code Online (Sandbox Code Playgroud)
主要思想来自Apache HttpClient 4.3中的忽略SSL证书
但仍然没有用.任何建议?
Zha*_*nlu 11
我正在使用“com.konghq:unirest-java:2.3.14”
现在有一个配置
Unirest.config().verifySsl(false);
Run Code Online (Sandbox Code Playgroud)
小智 10
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.build();
Unirest.setHttpClient(httpclient);
Run Code Online (Sandbox Code Playgroud)
这对我有用
这就是我最终解决了我的问题:
public static HttpClient makeClient(){
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
try {
schemeRegistry.register(new Scheme("https", 443, new MockSSLSocketFactory()));
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm);
return httpclient;
}
Run Code Online (Sandbox Code Playgroud)
我已经抓了一整天,我希望这可以帮助别人.
归档时间: |
|
查看次数: |
11815 次 |
最近记录: |