Socket socket = sslSocketFactory.createSocket();
socket.connect(new InetSocketAddress(hostname, port), 5000);
OutputStream out = socket.getOutputStream(); // this takes too long
connect
方法立即返回,但 getOutputStream
卡住了太久。
对于某些套接字地址并不总是太长
对于 HTTPS(端口 443),getOutputStream()
运行良好,但对于 HTTP(端口 80),它卡住了
最佳答案
For HTTPS (port 443) getOutputStream() worked well but for HTTP (port 80) it stuck
使用SSLSocket
?严肃点。端口 80 是明文。它不会响应 SSL 握手,至少不会像预期的那样。我很惊讶你没有得到握手异常。
对 HTTP 使用纯文本套接字,对 HTTPS 使用 SSL。实际上,您应该对两者都使用 HttpURLConnection
并停止尝试自己滚动。
关于java - Socket getOutputStream 耗时太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47007485/