Java HttpsURLConnection via Proxy not working while wget works fine

I am setting up data feeds from an external website and the code given below is working on dev, QA environments. It is failing only in production. One thing i noticed is that the WriteToLog in the Authenticator is not getting displayed on the console in Production environment whereas it does in Dev and QA where it is working. Could this be because of some missing library.

I am running on Java 1.8. I have tried the approach of using System.Setproperties to set all the variables https.proxyhost, proxyPort, ProxySet, user and password. But that didn't work either. Also wget and curl command are working on production after setting https_proxy environment variable.

try {
  Proxy ProxyName = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ProxyHostName, ProxyPortNum));
  Authenticator Credentials = new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
      WriteToLog.info(ProxyUserId + ProxyUserKey.toCharArray());
      return (new PasswordAuthentication(ProxyUserId, ProxyUserKey.toCharArray()));
    }
  };
Authenticator.setDefault(Credentials);
DataConnection = (HttpsURLConnection) RequestURL.openConnection(ProxyName);
String uname_pwd = ProxyUserId + ":" + ProxyUserKey;
String authString = "Basic " + new sun.misc.BASE64Encoder().encode(uname_pwd.getBytes());
WriteToLog.info(authString);
DataConnection.setRequestProperty("Proxy-Authorization",authString);
WriteToLog.info(DataConnection.getRequestMethod());
return DataConnection.getInputStream();
} catch (IOException ex) {
  WriteToLog.error("HttpsConnectionReturnStream():",ex);
  throw ex;
}

Error Message i get is:

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:992)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at com.project.fetch.WebAPIReader.HttpsConnectionReturnStream(WebAPIReader.java:186)
    at com.project.fetch.WebAPIReader.WebAPIRequestTokenSubmit(WebAPIReader.java:200)
    at com.project.run.LaunchApplication.LaunchOperation(LaunchApplication.java:50)
    at com.project.run.LaunchApplication.main(LaunchApplication.java:35)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:505)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
    ... 13 more

When i adding the System.setProperty("javax.net.debug","ssl,handshake") i get additional information:

main, WRITE: TLSv1.2 Handshake, length = 277
main, received EOFException: error
main, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
main, SEND TLSv1.2 ALERT:  fatal, description = handshake_failure

Can someone help me in figuring out how to debug or solve this problem ? I compared the Security Certificates on QA and Prod returned by the debug message. They are all matching. I tried changing Https.protocol to TLSv1 as well. But that to is not working.

Answers


I tried hardcoding the IP Address instead of the fully qualified hostname for the proxy server. The code worked perfectly fine after that. Not sure why the JVM isn't able to resolve the hostname when it works from command line using ping, wget or curl.


Need Your Help

Reload the page when visited by clicking Back button in Firefox

javascript jquery html html5 firefox

I have a page which animates entire body out of the viewer's screen as a transition. It works fine but when user clicks back button in the browser Firefox brings up the cached page from the history...