首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >createSocket、connect和connectionTimeout中的HTTPS问题

createSocket、connect和connectionTimeout中的HTTPS问题
EN

Stack Overflow用户
提问于 2016-08-03 10:02:11
回答 1查看 797关注 0票数 1

当我创建这样的SSL套接字时:

代码语言:javascript
复制
sslSocket = (SSLSocket) socketFactory.createSocket(host, port);

一切都正常。socketFactorySSLSocketFactory.getDefault()。但是,我想设置连接超时。因此,我将上面的代码更改为以下代码:

代码语言:javascript
复制
sslSocket = (SSLSocket) socketFactory.createSocket();
sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout);

但我得到了以下例外:

代码语言:javascript
复制
Exception in thread "main" jodd.http.HttpException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target;

原因例外:

代码语言:javascript
复制
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

在调试时,我注意到在第一种情况下,代码从请求的站点获取所有有效的证书。在第二种情况下,这些证书丢失了,因此出现了错误。

我还需要在我的2行更改中添加什么,才能使它像第一个一线版本一样工作呢?

完全STACKTRACE

代码语言:javascript
复制
jodd.http.HttpException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; <--- sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at jodd.http.HttpRequest.open(HttpRequest.java:667)
    at jodd.http.HttpRequest.open(HttpRequest.java:649)
    at jodd.http.HttpRequest._send(HttpRequest.java:747)
    at jodd.http.HttpRequest.send(HttpRequest.java:742)
    at jodd.JoddHttpTest.main(JoddHttpTest.java:56)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at jodd.http.net.SocketHttpConnectionProvider.createSSLSocket(SocketHttpConnectionProvider.java:153)
    at jodd.http.net.SocketHttpConnectionProvider.createHttpConnection(SocketHttpConnectionProvider.java:68)
    at jodd.http.HttpRequest.open(HttpRequest.java:665)
    ... 4 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
    ... 14 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    ... 20 more
---[cause]------------------------------------------------------------------------
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at jodd.http.net.SocketHttpConnectionProvider.createSSLSocket(SocketHttpConnectionProvider.java:153)
    at jodd.http.net.SocketHttpConnectionProvider.createHttpConnection(SocketHttpConnectionProvider.java:68)
    at jodd.http.HttpRequest.open(HttpRequest.java:665)
    at jodd.http.HttpRequest.open(HttpRequest.java:649)
    at jodd.http.HttpRequest._send(HttpRequest.java:747)
    at jodd.http.HttpRequest.send(HttpRequest.java:742)
    at jodd.JoddHttpTest.main(JoddHttpTest.java:56)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-03 11:55:30

这是答案。根据我的经验,这:

代码语言:javascript
复制
sslSocket = (SSLSocket) socketFactory.createSocket();
sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout);

不起作用(尽管你可以在任何地方看到这个解决方案!)。相反,我执行了以下操作:创建一个常规套接字,然后将其包装为SSL套接字:

代码语言:javascript
复制
Socket sock = new Socket();
sock.connect(new InetSocketAddress(host, port), connectionTimeout);
sslSocket = (SSLSocket)socketFactory.createSocket(sock, host, port, true);

其中socketFactory是一个SSLSocketFactory实例。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38740399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档