Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Apache HttpComponents for https requests: "peer not authenticated" and "handshake_failure" errors
    primarykey
    data
    text
    <p>I am attempting to make an HTTP GET call to a JBoss server using the Apache HttpComponents library. When I do this with an http URL, it works just fine, but when I use an https URL, it does not work. Here's the code I have:</p> <pre><code>public static String HttpGET(String requestURL, Cookie cookie) throws HttpException { DefaultHttpClient httpClient = new DefaultHttpClient(); if (cookie != null) { CookieStore store = new BasicCookieStore(); store.addCookie(cookie); ((AbstractHttpClient) httpClient).setCookieStore(store); } HttpGet httpGet = new HttpGet(requestURL); HttpResponse response = null; HttpEntity responseEntity = null; String responseBody = null; try { response = httpClient.execute(httpGet); // Do some more stuff... } catch (SSLPeerUnverifiedException ex) { // Message "peer not authenticated" means the server presented // a certificate that was not found in the local truststore. throw new HttpException("HTTP GET request failed; possible" + " missing or invalid certificate: " + ex.getMessage()); } catch (IOException e) { e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.getConnectionManager().shutdown(); } return responseBody; } </code></pre> <p>I am getting an <code>SSLPeerUnverifiedException</code> when I <code>execute()</code> my GET call. The error message is:</p> <pre><code>javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated </code></pre> <p>After some extensive Googling and searching through StackOverflow questions, I kept seeing this suggestion, so I added this wrapper around my DefaultHttpClient, like this:</p> <pre><code>private static HttpClient wrapClient(HttpClient httpClient) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) { } public void checkServerTrusted(X509Certificate[] xcs, String string) { } public X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier verifier = new X509HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return false; } @Override public void verify(String arg0, SSLSocket arg1) throws IOException { } @Override public void verify(String arg0, X509Certificate arg1) throws SSLException { } @Override public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException { } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory socketFactory = new SSLSocketFactory(ctx); socketFactory.setHostnameVerifier(verifier); Scheme sch = new Scheme("https", 443, socketFactory); httpClient.getConnectionManager().getSchemeRegistry().register(sch); return httpClient; } catch (Exception ex) { ex.printStackTrace(); return null; } } </code></pre> <p>This just produces a different error, however:</p> <pre><code>javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure </code></pre> <p>I believe the certs are set up correctly because other code that was written using the Jersey libraries to make connections to this server is able to do so successfully. However, I'm not seeing what I'm doing incorrectly with Apache HttpComponents. Any ideas? I apologize if I'm making obvious mistakes, I am new to SSL and don't have a full understanding of what I'm doing yet. Thanks for any help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload