为单个连接禁用 SSLHandshakeException [英] Disable SSLHandshakeException for a single connection

查看:17
本文介绍了为单个连接禁用 SSLHandshakeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找类似于这个答案的解决方案,但更安全.我想禁用证书验证,但仅针对单个请求(这是我所需要的).所以它应该做以下一项或多项

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆