繁体   English   中英

如何在已打开的SSL Session java中打开ssl socket

[英]How to open ssl socket in already opened SSL Session java

我打开了HttpsURLConnection。 如何将我的ssl套接字“连接”到此连接,意味着不要再进行另一次ssl握手,也不要更改我的密码。

URL url = new URL ("https://example.com:8080");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.connect();

SSLSocketFactory ssf = connection.getSSLSocketFactory();
SSLSocket socket = (SSLSocket) ssf.createSocket("example.com", 8080);

//这里我想用socket发送消息,它将与https连接在同一个ssl会话中。

BufferedWriter out = new BufferedWriter(
                        new OutputStreamWriter(s.getOutputStream()));
out.write("Hello");
out.flush();
out.close();

是不是真的在Java中做这样的事情?

默认情况下,它应该以这种方式发生,只要会话保持有效,并且只要您没有使用SSLContexts.

AFAIK,您无法将新套接字附加到现有SSL会话。 如果要在原始SSL会话上发送/读取数据,请使用URLConnection对象创建的原始套接字连接。 您可以使用URLConnection.getInputStream()URLConnection.getOutputStream()方法为其获取输入/输出流:

URL url = new URL ("https://example.com:8080");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.connect();

...

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
out.write("Hello");
out.flush();
out.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM