我从这边使用SSL Socket和Trustmanager 自签名SSL
但我不断收到以下错误:
09-28 19:52:41.942:WARN/System.err(10101):javax.net.ssl.SSLHandshakeException:org.bouncycastle.jce.exception.ExtCertPathValidatorException:无法验证证书签名.
怎么了?我已经在stackoverflow上检查了不同的帖子,但我似乎无法让它工作.
我的代码:
SchemeRegistry schemeRegistry = new SchemeRegistry();
// http scheme
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// https scheme
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 1);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(1));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "utf8");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("www.example.com", AuthScope.ANY_PORT),
new UsernamePasswordCredentials("user", "password"));
clientConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
context = new BasicHttpContext();
context.setAttribute("http.auth.credentials-provider", credentialsProvider);
DefaultHttpClient client = new DefaultHttpClient(clientConnectionManager, params);
HttpGet get = new HttpGet("https://www.example.com/web/restricted/form/formelement=512663");
HttpResponse response = client.execute(get, …Run Code Online (Sandbox Code Playgroud) 我试图接受所有证书,和/或使用Apache HTTPClient 4.5版接受自签名证书(这里的教程链接)
我已经从SO上的一堆帖子中解决了这个问题.到目前为止,他们都没有工作过.
我一直收到这个错误: Error while trying to execute request. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
Apache文档:
相关的StackOverflow问题 - 以下是我尝试过的解决方案的一些链接:
请注意,在所有这些示例中,我还传递了我之前定义的cookie存储和代理凭据提供程序.这些都在工作,我只是想添加SSL支持.
使用创建我自己的ssl上下文SSLContextBuilder并信任所有自签名策略TrustSelfSignedStrategy.
SSLContextBuilder sshbuilder = new SSLContextBuilder();
sshbuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sshbuilder.build());
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.setDefaultCookieStore(cookieStore)
.setSSLSocketFactory(sslsf)
.build();
Run Code Online (Sandbox Code Playgroud)
结果:没有用.拿到Error while trying to …
java ssl httpclient apache-commons-httpclient apache-httpclient-4.x