如何在java中的ssl connect中进行握手?
当我没有使用ssl连接时,我使用了这段代码
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
//translate bytes of request to string
data = new Scanner(in,"UTF-8").useDelimiter("\\r\\n\\r\\n").next();
System.out.println("data="+data);
Matcher get = Pattern.compile("^GET").matcher(data);
if (get.find()) {
Matcher match = Pattern.compile("Sec-WebSocket-Key: (.*)").matcher(data);
match.find();
try {
byte[] response = ("HTTP/1.1 101 Switching Protocols\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Accept: "
+ DatatypeConverter
.printBase64Binary(
MessageDigest
.getInstance("SHA-1")
.digest((match.group(1) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
.getBytes("UTF-8")))
+ "\r\n\r\n")
.getBytes("UTF-8");
out.write(response, 0, response.length);
System.out.println("-----response1----- "+"HTTP/1.1 101 Switching Protocols\r\n"+ "Connection: Upgrade\r\n"+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Accept: "+ DatatypeConverter.printBase64Binary(MessageDigest.getInstance("SHA-1").digest((match.group(1) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes("UTF-8")))+ "\r\n\r\n");
}
}
我如何在下一个代码中进行握手?
SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =
(SSLServerSocket) sslserversocketfactory.createServerSocket(9999);
System.out.println("Server has started on 127.0.0.1:80.\r\nWaiting for a connection...");
SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
InputStream inputstream = sslsocket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
InputStream in = sslsocket.getInputStream();
OutputStream out = sslsocket.getOutputStream();
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
System.out.println("5");
String string = null;
while ((string = bufferedreader.readLine()) != null) {
System.out.println("6");
System.out.println(string);
System.out.flush();
}
没有握手我有错误
index.js:1 WebSocket连接到' wss://127.0.0.1:9999 /'失败:WebSocket开放握手被取消