Java HttpsURLConnection via Proxy 不起作用,而 wget 工作正常



我正在设置来自外部网站的数据馈送,下面给出的代码适用于开发,QA环境。它仅在生产中失败。我注意到的一件事是,身份验证器中的 WriteToLog 不会显示在生产环境中的控制台上,而在它工作的 Dev 和 QA 中会显示。这可能是因为缺少一些库。

我在Java 1.8上运行。我已经尝试了使用System.Setproperties来设置所有变量https.proxyhost,proxyPort,ProxySet,user和password的方法。但这也没有用。此外,wget 和 curl 命令在设置环境变量后https_proxy正在生产中工作。

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,handshake")时,我得到了额外的信息:

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。但这是行不通的。

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

最新更新