簡體   English   中英

wget正常工作時,通過代理的Java HttpsURLConnection無法正常工作

[英]Java HttpsURLConnection via Proxy not working while wget works fine

我正在從外部網站設置數據供稿,下面給出的代碼在開發,質量檢查環境中有效。 它僅在生產中失敗。 我注意到的一件事是,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,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從命令行運行時無法解析主機名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM