在SSLSocket getInputStream()上没有结果。

时间:2018-07-16 05:02:11

标签: java ssl sslsocketfactory

我正在尝试使用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();
}

1 个答案:

答案 0 :(得分:0)

这里有辅助功能!

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