java - 在握手完成之前阻止 SSLSocket?

标签 java android ssl

有没有办法让 SSLSocket 在不使用 addHandshakeListener() 方法的情况下等待握手完成?我希望它在完成握手之前一直阻塞。

编辑: 稍微澄清一下:最初使用常规(非 SSL)套接字(这是必需的,因为在实际握手发生之前必须将数据包发送到服务器)。使用 SSlSocketFactory.createSocket 将该套接字“转换”为 SSLSocket。 . 我的问题是现在是否有办法同步进行握手。

最佳答案

SSLSocket.startHandshake() 是一个同步调用,当您第一次在对等端之间开始握手时,它将阻塞调用线程,直到握手完成:

... ...

// initial handshake between peers, don't need Listener:
sslSocket.startHandshake(); // <- this will block the calling thread.
// handshake is completed at this point

... ...

这在 Android API 中没有完整记录, 见原文 Java API对于完整的 javadoc:

Starts an SSL handshake on this connection. Common reasons include a need to use new encryption keys, to change cipher suites, or to initiate a new session. To force complete reauthentication, the current session could be invalidated before starting this handshake.

If data has already been sent on the connection, it continues to flow during this handshake. When the handshake completes, this will be signaled with an event. This method is synchronous for the initial handshake on a connection and returns when the negotiated handshake is complete. Some protocols may not support multiple handshakes on an existing socket and may throw an IOException.

如果你在对等点之间多次操纵握手,情况会变得相当复杂,在某些情况下(当你需要 Listener 时)它可能是一个异步调用,查看这个在线文档 Fundamental Networking in Java了解更多详情。

关于java - 在握手完成之前阻止 SSLSocket?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12956449/

相关文章:

Informatica BDM 中的 Java 转换。将日期范围记录拆分为多条记录,每月一条记录

java - 未找到 Android Studio 注释抽象处理器

java - 在 LibGdx 应用程序上的屏幕之间切换

ssl - 将 keystore 与 Talend Administration Center 结合使用

java - 生成字符数组的所有排列

java - 如何根据单个节点的位置对齐 GridPane?

android - 谷歌登录,当用户被要求再次登录帐户时如何正确处理错误情况

javascript - 无法在 chrome-android 网站上执行 'play' : API can only be initiated by a user gesture. 上的 'HTMLMediaElement'

php - .htaccess 重写为 https

json - 我们如何通过网络安全地将 API key 和 secret token 交付给用户?