1

I am trying to get some data (json data -> restful server) from a HTTPS server with basic authentification using the Apache httpclient. The SSL certificate is selfsigned. The server is responding very well to a browser call and also when using curl.

However using the java Apache httpclient, that's another story.

Client side :

The basic authentification is working : the server sends me 401 errors if forget the authorization header or if I set the wrong base64 encoded login:password.

The https/SSL part is working : I am successfully getting data from online restful json server but sadly no way to find an online restful json server with basic authentification for testing purpose...

        try {

            CloseableHttpClient httpClient = null;
            try {
                httpClient = HttpClients.custom()
                        .setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContexts.custom()
                                        .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                                        .build()
                                )
                        )
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                        .build();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
            } catch (KeyStoreException e) {
                e.printStackTrace();
            }

            HttpGet getRequest = new HttpGet("https://localhost:5050/getdata");
            getRequest.addHeader("Accept", "*/*");
            getRequest.addHeader("Authorization", "Basic UBJ0aHVyOmFo0XElYHU=");
            HttpResponse response = httpClient.execute(getRequest);

Debugging is telling me :

Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response

True! It's not a valid HTTP response that I would like to get, it's a valid HTTPS response!

I guess that I am missing something...

4
  • "sadly no way to find an online restful json server with basic authentification" - Try: httpbin.org/basic-auth/user/passwd Commented Apr 26, 2017 at 21:56
  • Is this code snippet complete? You don't know where you tried to read anything from the response or a stack trace that shows where the exception occurred Commented Apr 27, 2017 at 3:33
  • @Vasan. Tested with httpbin.org/basic-auth/user/passwd, it's working perfectly... Commented Apr 27, 2017 at 18:51
  • @Alex, the exception is occuring on the last line of my code snippet : HttpResponse response = httpClient.execute(getRequest); Commented Apr 27, 2017 at 18:53

1 Answer 1

2

Solved!

The error was from the server side : my response did not include any headers....

httpclient seems to like well made responses from a server. That's not true for a browser or for curl : garbage they can receive , display they will !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.