Java 和 HttpsUrlConnection 忽略 ssl 证书
Java and HttpsUrlConnection ignoring ssl certificates
1)我正在尝试通过忽略 ssl 证书
来使用 https url
2)我试图点击 https url 请求到 Google 地理定位 api url 但我越来越下面的响应表明我正在制作错误的程序。
因为我通过 Postman 尝试了 url,它显示了正确的输出。
HttpResponseProxy{HTTP/1.1 404 Not Found [Cache-Control: no-cache, no-store, max-age=0, must-revalidate, Pragma: no-cache, Expires: Mon, 01 Jan 1990 00:00:00 GMT, Date: Sun, 26 Nov 2017 07:38:48 GMT, Vary: Origin, Vary: X-Origin, Content-Type: text/html; charset=UTF-8, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35", Transfer-Encoding: chunked] org.apache.http.client.entity.DecompressingEntity@5c18298f}
我也使用过其他资源,但找不到相同的提示。
这是我的代码
public void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect()
throws Exception {
String HOST_WITH_SSL="https://www.googleapis.com/geolocation/v1/geolocate?key=XXX";
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (certificate, authType) -> true).build();
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpGet httpGet = new HttpGet(HOST_WITH_SSL);
httpGet.setHeader("Accept", "application/json");
HttpResponse response = client.execute(httpGet);
System.out.println(response);
}
Can anyone guide how to hit https url correctly?
代码中的 URL 需要 POST 请求,而您发送的是 GET。
使用 GET 请求它 returns 即使在 Postman 中也是 404。
更改代码以使用 HttpPost 解决了您的问题。
Google 有一个有效的证书,你可能不应该尝试忽略证书。
根据 helospark 的建议,我将 get Url 更改为 post。
public void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect()
throws Exception {
String HOST_WITH_SSL = "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyD-5wmiRDIWRCmUCiI5utqzoro8cbNyyi8";
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (certificate, authType) -> true).build();
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpPost httpPost = new HttpPost(HOST_WITH_SSL);
HttpResponse response = client.execute(httpPost);
ResponseHandler<String> handler = new BasicResponseHandler();
String body = handler.handleResponse(response);
System.out.println(response);
System.out.println(body);
}
1)我正在尝试通过忽略 ssl 证书
来使用 https url2)我试图点击 https url 请求到 Google 地理定位 api url 但我越来越下面的响应表明我正在制作错误的程序。
因为我通过 Postman 尝试了 url,它显示了正确的输出。
HttpResponseProxy{HTTP/1.1 404 Not Found [Cache-Control: no-cache, no-store, max-age=0, must-revalidate, Pragma: no-cache, Expires: Mon, 01 Jan 1990 00:00:00 GMT, Date: Sun, 26 Nov 2017 07:38:48 GMT, Vary: Origin, Vary: X-Origin, Content-Type: text/html; charset=UTF-8, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35", Transfer-Encoding: chunked] org.apache.http.client.entity.DecompressingEntity@5c18298f}
我也使用过其他资源,但找不到相同的提示。
这是我的代码
public void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect()
throws Exception {
String HOST_WITH_SSL="https://www.googleapis.com/geolocation/v1/geolocate?key=XXX";
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (certificate, authType) -> true).build();
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpGet httpGet = new HttpGet(HOST_WITH_SSL);
httpGet.setHeader("Accept", "application/json");
HttpResponse response = client.execute(httpGet);
System.out.println(response);
}
Can anyone guide how to hit https url correctly?
代码中的 URL 需要 POST 请求,而您发送的是 GET。
使用 GET 请求它 returns 即使在 Postman 中也是 404。
更改代码以使用 HttpPost 解决了您的问题。
Google 有一个有效的证书,你可能不应该尝试忽略证书。
根据 helospark 的建议,我将 get Url 更改为 post。
public void givenIgnoringCertificates_whenHttpsUrlIsConsumed_thenCorrect()
throws Exception {
String HOST_WITH_SSL = "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyD-5wmiRDIWRCmUCiI5utqzoro8cbNyyi8";
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, (certificate, authType) -> true).build();
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpPost httpPost = new HttpPost(HOST_WITH_SSL);
HttpResponse response = client.execute(httpPost);
ResponseHandler<String> handler = new BasicResponseHandler();
String body = handler.handleResponse(response);
System.out.println(response);
System.out.println(body);
}