简体   繁体   English

在HttpURLConnection中容纳HTTPS连接

[英]Accommodate HTTPS Connections in HttpURLConnection

I am using HttpURLConnection to do POST requests. 我正在使用HttpURLConnection进行POST请求。

I read HttpsURLConnection extends HttpURLConnection with support for https-specific features such as SSL 我读了HttpsURLConnection扩展了HttpURLConnection并支持https特定功能(例如SSL)

If HttpsURLConnection use same methods in HttpURLConnection(parent class), it super calls the methods from HttpURLConnection. 如果HttpsURLConnection在HttpURLConnection(父类)中使用相同的方法,则它将从HttpURLConnection超级调用这些方法。

        URL url = new URL(callBackUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setConnectTimeout(setConnectTimeout);
        connection.setReadTimeout(setConnectTimeout);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream());
        os.write(data.toString());

        os.flush();
        os.close();

        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));

            while ((output = br.readLine()) != null) {
                stringBuffer.append(output);

            }
        }

However, when I use a HTTPS Url, I keep getting the error below; 但是,当我使用HTTPS网址时,我不断收到以下错误;

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) ~[na:1.8.0_181]
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964) ~[na:1.8.0_181]
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328) ~[na:1.8.0_181]
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322) ~[na:1.8.0_181]

Question, how do I handle both SSL and non SSL connection as is the case in PHP where when using CURL, you set ignore SSL check 问题,我如何处理SSL和非SSL连接,就像在PHP中那样,当使用CURL时,您设置了忽略SSL检查

curl_setopt($cHandler, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($cHandler, CURLOPT_SSL_VERIFYPEER, true);

I am on Linux, Spring framework and TomCat Web Server. 我在Linux,Spring框架和TomCat Web服务器上。

Anyone? 任何人?

For the HTTPS things to work first, you would need to add your SSL Certificate into your Java's trust store... For that, you'll have to follow the following steps: 为了使HTTPS能够首先运行,您需要将SSL证书添加到Java的信任库中。为此,您必须执行以下步骤:

Generally your truststore file of the used JVM would be located at %JAVA_HOME%\\lib\\security\\cacerts . 通常,使用的JVM的信任库文件将位于%JAVA_HOME%\\lib\\security\\cacerts

You might first take a look if your certificate is already added to the truststore by running the following command: 您可能首先通过运行以下命令来查看您的证书是否已经添加到信任库中:

keytool -list -keystore "%JAVA_HOME%/jre/lib/security/cacerts"

Once you confirm that it is not added to your Java trust store, you can use the following command to add it: 一旦确认未将其添加到Java信任库中,就可以使用以下命令将其添加:

keytool -import -noprompt -trustcacerts -alias <AliasName> -file   <certificate> -keystore <KeystoreFile> -storepass <Password>

Once the following steps are done, you should be able to run your program without running into your HTTPS related issues. 完成以下步骤后,您应该能够运行程序,而不会遇到与HTTPS相关的问题。

Hope that helps! 希望有帮助!

EDIT-1 : The above method helps adding an SSL Certificate on a Windows environment. EDIT-1 :以上方法有助于在Windows环境中添加SSL证书。 Please check the following article for a Linux variant... 请查看以下有关Linux变体的文章 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM