gpt4 book ai didi

java - 不是 SSLSocket getInputStream() 的结果。

转载 作者:太空宇宙 更新时间:2023-11-03 14:32:36 24 4
gpt4 key购买 nike

我正在尝试使用 SSLSocket 通过 HTTPS 协议(protocol)连接到服务器。在浏览器中,响应是正常的,但我的代码没有得到响应。如果有人有解决方案,请帮助我!谢谢

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;
}

关于java - 不是 SSLSocket getInputStream() 的结果。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51354864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com