禁用单个连接的SSLHandshakeException [英] Disable SSLHandshakeException for a single connection
问题描述
我正在寻找类似于此答案的解决方案,但更安全。我想禁用证书验证,但仅针对单个请求(这是我所需要的)。所以它应该做以下一个或多个
I'm looking for a solution similar to this answer, but much safer. I'd like to disable the certificate validation, but for a single request only (which is all I need). So it should do one or more of the following
- 当一个请求完成后返回安全状态
- 仅禁用给定URL的验证
- (可能)仅针对一个线程使用不安全设置
我真的很想知道这个问题(得分-2)与原来的问题(得分+46)有什么不对,当我要求更安全的解决方案时。有人可以解释一下吗?
解释为什么我需要这个:有一个有效的服务器证书,通常,它会被使用。但我需要向 localhost
发送一个请求,它也必须在开发人员计算机上运行。它必须是 https
,因为没有 http
支持。
To explain why I need this: There's a valid server certificate and normally, it gets used. But I need to send one request to localhost
and it has to work on a developer machine, too. It must be https
as there's no http
support.
推荐答案
只需使用实例方法 setX()
而不是静态 setDefaultX()
:
Just use instance methods setX()
instead of static setDefaultX()
:
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(...);
connection.setHostnameVerifier(...);
这篇关于禁用单个连接的SSLHandshakeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!