2017-11-26 4 views
-1

1)私は、URLのSSL証明書私はGoogleのジオロケーションAPI URLにhttpsのURL要求をヒットしようとしてい )を無視して、HTTPSを消費しようとしていますが、私は示す応答の下に取得しています私は間違ったプログラムを作っています。JavaとのHttpsURLConnection無視してSSL証明書は、

私は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] [email protected]} 

私は他のソースも使用していますが、同じヒントを見つけることができませんでした。ここで

は私のコードは

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

で誰もが正しくHTTPSのURLをヒットする方法を導くことはできますか?

+0

なぜ、HTTPSを使用しているのですか? – EJP

+0

@EJPあなたは正しいです。私は初心者ですので、httpsサーバと通信すると思っています。あなたはhttpsしか使えません。 –

答えて

1

コードのURLにはPOSTリクエストが必要で、あなたはGETを送信しています。

GETリクエストでは、Postmanでも404を返します。

HttpPostを使用するようにコードを変更すると、問題が解決します。

Googleには有効な証明書がありますが、証明書を無視しないでください。

+0

私は変更を行い、答えを得ました。感謝祭 –

0

helosparkの提案によれば、私はget Urlをポストするように変更しました。

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