0

Tried searching our blog but was not able to find some pointers so posting,

I was just working on a httpsClient Post method code as below and facing IOException. little Pointers may help , dont require detailed answers.

HttpClient httpclient = new HttpClient();         
              request = new PostMethod("https://ref.net/auth/token?grant_type=password");             
              request.setRequestHeader("Authorization", "Basic KolokYrZTo=");
              request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

          NameValuePair[] data = {
                  new NameValuePair("username", "abc"),
                  new NameValuePair("password", "123"),
                  new NameValuePair("grant_type", "password"),
          };

          request.setRequestBody(data);
          httpclient.executeMethod(request);
 } catch (IOException e) {
                  e.printStackTrace();
                  logger.error( "IOException in post()...."+e.getMessage());                
              }

I am running this code on eclipse with commons-httpClient 3.1.jar it runs perfectly fine, But when i move my code to server and try to run the same code, the httpclient.executeMethod(request); throws IOException. To my surprise the e.printStackTrace(); does not prints as e object itself is null How do i catch this exception and debug this, I am doing something wrong here.

On my local i have Java1.8 but running at compiler compliance level 1.6- On server. I tried running both 1.8 and 1.6 same error.

java_java
  • 1
  • 1
  • is it related to some certificates, which we may have to configure on server and are present on local ?, i did not add any certificates though – java_java Sep 25 '17 at 18:26
  • If `e` is really`null` as you say, the whole thing should subsequently throw a `NullPointerException` when you do `e.printStackTrace` so I have doubts that this is actually the case. I assume that you are referring to what you see in your log. But there you are just adding `e.getMessage()` which might be null. Log the complete exception (so the type of exception being thrown and the stacktrace are logged as well) and add it here, so more can be said about your problem. – Lothar Sep 25 '17 at 19:15
  • javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) – java_java Sep 25 '17 at 19:39

1 Answers1

0

With the error message you added as comment it is clear what's happening here. The server you're connecting to is using a server certificate that the JVM you're using on the command shell doesn't know about.

If you search for this error message in combination with commons httpclient you can find a couple of pages discussion this, e.g. one at StackOverflow.

Lothar
  • 5,323
  • 1
  • 11
  • 27