java请求https_Java 发送HTTPS请求到非信任网站

1.Overview

HTTPS pages typically use one of two secure protocols to encrypt communications - SSL (Secure Sockets Layer) or TLS (Transport Layer Security).When you request a HTTPS connection to a webpage, the website will initially send its SSL certificate to your browser. This certificate contains the public key needed to begin the secure session. Based on this initial exchange, your browser and the website then initiate the 'SSL handshake'. The SSL handshake involves the generation of shared secrets to establish a uniquely secure connection between yourself and the website.

In overview, the steps involved in the SSL handshake are shown in Fig 1:

1eca1c45c4deaad3e9db39b60bec1dbf.png

2.Sending HTTPS Requests from Java Program

When writing unit tests, we may need to send HTTPS requests to some websites and get the results. But when the certificate from these websites cannot be verified, handshake exception would be thrown. To send HTTPS requests successfully, you can do as following steps:

1.Download the certificate of the website you want to visit.

3cb35d3a6f159eefb0be44c9b6491f13.png

2.Use keytool to store the certification in your java trustStore. (default password "changeit")

(1) copy ./testcert.cer to /path/to/your/JAVA_HOME/jre/lib/security

(2) keytool -import -trustcacerts -alias testCert -keystore cacerts -file testcert.cer

(3) check the certificate imported successfullly

8e63c2f60375a0b8373b6ae860a74f00.png

3. Check the TLS protocol version of the website you want to visit. You can use the website bellow to get all the ssl and tsl information(qarot-analytics.sflab.ondemand.com e.g.).

https://www.ssllabs.com/ssltest/analyze.html?d=qarot-analytics.sflab.ondemand.com

9b7031bbe4a606c255bb4b80210a46d6.png

4.Set the Certification and TLS version for your JRE

(1) Use Java Code

Properties systemProps = System.getProperties();

systemProps.put( "javax.net.ssl.trustStore", "\path\to\your\JVA_HOME\jre\lib\security\cacerts");

systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");

System.setProperty("https.protocols", "TLSv1.2");

System.setProperties(systemProps);

(2) Use Java -D parameter

-Djavax.net.ssl.trustStore="%JAVA_HOME%\jre\lib\security\cacerts"

-Djavax.net.ssl.trustStorePassword="changeit"

-Dhttps.protocols=TLSv1.2

-Djavax.net.debug=all //Log all the information

5.Use SystemProps when Creating HttpClient

public class HTTPSTest {

@Test

public void sendHttpsRequestByHttpClientWithJDK7() {

Properties systemProps = System.getProperties();

systemProps.put( "javax.net.ssl.trustStore", "C:\\Java\\jvm_7.1.041\\jvm_7\\jre\\lib\\security\\cacerts");

systemProps.put( "javax.net.ssl.trustStorePassword", "changeit");

System.setProperty("https.protocols", "TLSv1.2");

System.setProperties(systemProps);

CloseableHttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build();

HttpGet httpGet = new HttpGet("https://qarot-analytics.sflab.ondemand.com");

try {

CloseableHttpResponse response = httpClient.execute(httpGet);

response.getEntity();

} catch (IOException e) {

e.printStackTrace();

}

}

3.Troubleshooting

During the handshake of client and server, handshake exception may occur.

The handshake failure could have occurred due to various reasons:

1. Incompatible cipher suites in use by the client and the server. This would require the client to use (or enable) a cipher suite that is supported by the server.

2. Incompatible versions of SSL in use (the server might accept only TLS v1, while the client is capable of only using SSL v3). Again, the client might have to ensure that it uses a compatible version of the SSL/TLS protocol.

3. Incomplete trust path for the server certificate; the server's certificate is probably not trusted by the client. This would usually result in a more verbose error, but it is quite possible. Usually the fix is to import the server's CA certificate into the client's trust store.

4. The cerificate is issued for a different domain. Again, this would have resulted in a more verbose message, but I'll state the fix here in case this is the cause. The resolution in this case would be get the server (it does not appear to be yours) to use the correct certificate.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值