不是SSLSocket getInputStream()的结果。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不是SSLSocket getInputStream()的结果。相关的知识,希望对你有一定的参考价值。
我正在尝试使用SSLSocket通过HTTPS协议连接到服务器。在浏览器中,响应是正常的,但我的代码没有得到答案。如果有人有解决方案,请帮助我!谢谢
public static void main(String[] args) throws IOException {
String urlString = "https://host";
URL url = new URL(urlString);
Security.addProvider(
new com.sun.net.ssl.internal.ssl.Provider());
SSLSocketFactory factory = null;
try {
factory = ooooo();
} catch (Exception e) {
e.printStackTrace();
}
SSLSocket socket =
(SSLSocket)factory.createSocket(url.getHost(),8443);
socket.setNeedClientAuth(true);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())));
socket.addHandshakeCompletedListener(new HandshakeCompletedListener() {
@Override
public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent) {
System.out.println(handshakeCompletedEvent.getSession());
}
});
socket.startHandshake();
write(out);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close()
out.close();
socket.close();
}
答案
这里有帮手功能!
private static void write(PrintWriter out){
out.println("GET ... HTTP/1.1");
out.println("Host: host");
out.println("Connection: keep-alive");
out.println("Cache-Control: max-age=0");
out.println("Upgrade-Insecure-Requests: 1");
out.println("User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/67.0.3396.99 Safari/537.36");
out.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
out.println("Accept-Encoding: gzip, deflate, br");
out.println("Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7");
out.println();
out.flush();
}
private static SSLSocketFactory ooooo() throws Exception {
String clientPassword = "123";
String clientCertificatePath = "my_cert.pfx";
String trustStorePath = "cacerts";
String trustStorePassword = "changeit"; // default trust store password
KeyStore clientStore = KeyStore.getInstance("PKCS12");
clientStore.load(new FileInputStream(clientCertificatePath), clientPassword.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(clientStore, clientPassword.toCharArray());
KeyManager[] kms = kmf.getKeyManagers();
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(new FileInputStream(trustStorePath), trustStorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustStore);
TrustManager[] tms = tmf.getTrustManagers();
SSLContext sslContext = null;
sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(kms, tms, new SecureRandom());
SSLSocketFactory lSchemeSocketFactory=sslContext.getSocketFactory();
return lSchemeSocketFactory;
}
以上是关于不是SSLSocket getInputStream()的结果。的主要内容,如果未能解决你的问题,请参考以下文章
尝试发送到 SSLServerSocket 时出现 SSLSocket 异常握手错误