通过代理服务器的Java HttpsURLConnection在wget正常工作时无效

时间:2016-01-24 16:19:17

标签: java ssl proxy httpsurlconnection

我正在设置来自外部网站的数据源,下面给出的代码适用于开发,QA环境。它只在生产中失败了。我注意到的一件事是Authenticator中的WriteToLog没有显示在生产环境中的控制台上,而它在Dev和QA中显示它正在工作。这可能是因为一些遗失的图书馆。

我在Java 1.8上运行。我尝试过使用System.Setproperties设置所有变量https.proxyhost,proxyPort,ProxySet,用户和密码的方法。但那也没有用。在设置https_proxy环境变量后,wget和curl命令也正在进行生产。

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

我收到的错误消息是:

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

当我添加System.setProperty(" javax.net.debug"," ssl,握手")时,我会收到更多信息:

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

有人可以帮我弄清楚如何调试或解决这个问题吗?我比较了调试消息返回的QA和Prod上的安全证书。它们都是匹配的。我也尝试将Https.protocol更改为TLSv1。但那是行不通的。

1 个答案:

答案 0 :(得分:0)

我尝试对IP地址进行硬编码,而不是代理服务器的完全限定主机名。之后代码完美无缺。不知道为什么JVM在使用ping,wget或curl从命令行工作时无法解析主机名。