【问题标题】:java HttpURLConnection tunneling error 407java HttpURLConnection隧道错误407
【发布时间】:2019-02-10 23:24:54
【问题描述】:

我在我的一个程序中收到了主题提到的错误代码。我在 Stackoverflow 上四处寻找解决方案,并尝试遵循 Ilana PlatonovPPr 报告的问题的建议。不幸的是,这些并没有解决错误。

下一步,我只是尝试运行Ilana Platonov 中提供的代码以及解决方案(在那里接受)。我的代码: 包示例;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;


public class MyProxyPass {
    public MyProxyPass( String proxyHost, int proxyPort, final String userid, final String password, String url ) {

try {
        /* Create a HttpURLConnection Object and set the properties */
        URL u = new URL( url );
        Proxy proxy = new Proxy( Proxy.Type.HTTP, new InetSocketAddress( proxyHost, proxyPort ) );
        HttpURLConnection uc = (HttpURLConnection) u.openConnection( proxy );

        Authenticator.setDefault( new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                if (getRequestorType().equals( RequestorType.PROXY )) {
                    return new PasswordAuthentication( userid, password.toCharArray() );
                }
                return super.getPasswordAuthentication();
            }
        } );
        uc.connect();
        /* Print the content of the url to the console. */
        showContent( uc );
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

private void showContent( HttpURLConnection uc ) throws IOException {
    InputStream i = uc.getInputStream();
    char c;
    InputStreamReader isr = new InputStreamReader( i );
    BufferedReader br = new BufferedReader( isr );
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println( line );
    }
}

public static void main( String[] args ) {
    String proxyhost = "xxx.xxx.xx.xx";
    int proxyport = xxxx;
    final String proxylogin = "JOKER";
    final String proxypass = "passxxx";

    String url = "http://www.google.de";
    String surl = "https://www.google.de";

    System.setProperty("javax.net.ssl.trustStore", "C:\\Users\\JOKER\\Documents\\eclipse-jee-photon-R-win32\\eclipse");
    System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

    //new MyProxyPass( proxyhost, proxyport, proxylogin, proxypass, url );  // uncomment this line to see that the https request works!
    //System.out.println( url + " ...ok" );   // uncomment this line to see that the https request works!
    new MyProxyPass( proxyhost, proxyport, proxylogin, proxypass, surl );
    System.out.println( surl + " ...ok" );
}
}

但是,我仍然收到其他人报告的相同错误:

   java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
https://www.google.de ...ok 
at    sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2124)
at    sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:183)
at    sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at    examples.MyProxyPass.<init>(MyProxyPass.java:32)
at examples.MyProxyPass.main(MyProxyPass.java:63)

什么对别人有用,对我没用:

  1. 关于将变量 jdk.http.auth.tunneling.disabledSchemes 和 jdk.http.auth.proxying.disabledSchemes 设置为空白的常见建议不起作用。我在 ..\Java\jdk1.8.0_112\jre\lib\net.properties 文件中将这些设置为空白。

  2. 使用Andreas Panagiotidis 提出的各种 JVM Args 也无济于事。

  3. 来自https://bugs.eclipse.org/bugs/show_bug.cgi?id=422665 我还尝试排除可能存在冲突的HTTP 库。 -Dorg.eclipse.ecf.provider.filetransfer.excludeContributors=org.eclipse.ecf.provider.filetransfer.httpclient4

我正在使用:

  • Eclipse IDE 光子发布 (4.8.0),
  • java 版本“1.8.0_112”(内部版本 1.8.0_112-b15)
  • Windows 10
  • 我支持公司代理,并且在这方面花了整整两天时间,但没有成功。 :(

如果我应该附上任何其他日志,请告诉我。

【问题讨论】:

    标签: java proxy http-tunneling http-status-code-407


    【解决方案1】:

    公司代理是罪魁祸首!一旦我解决了这些问题,我上面发布的代码就可以顺利运行。

    【讨论】:

      【解决方案2】:

      我遇到了另一种情况,我遇到了同样的错误(使用正确的代理详细信息)。

      Could not retrive data: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"
      

      这次错误是由于 Java 版本 1.8.0_111 造成的。自此 Java 更新以来,HTTPS 隧道的基本身份验证已禁用。这是通过将协议添加到 disabledScheme 列表来完成的,即 lib/net.properties 文件中的 jdk.http.auth.tunneling.disabledSchemes=Basic。

      要解决此错误,请通过将变量 jdk.http.auth.tunneling.disabledSchemes 设置为 emtpy 字符串来启用基本身份验证: '-Djdk.http.auth.tunneling.disabledSchemes='

      Enable/Disable Basic Authentication for HTTPS

      【讨论】:

        【解决方案3】:

        我刚刚解决了同样的问题。

        我的应用程序创建了一个 HTTP 传输实例,然后使用它向服务器发出请求。可以选择将此传输配置为使用代理服务器。所以transport的配置方法中有一段代码:

        System.setProperty("jdk.http.auth.tunneling.disabledSchemes","");
        Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                   return new PasswordAuthentication(proxyUserName, proxyUserPassword.toCharArray());
                }
            });
        

        看起来不错,但是通过代理服务器的所有请求都失败并出现此异常:

        java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required"

        然后我注意到我的应用程序在创建传输(并执行上面的行)之前向服务器发出请求以获取一些数据。

        现在查看堆栈跟踪中的这一行:

        at    sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:2124)
        

        如果你查看这个类的源代码,你会发现static 块,其中jdk.http.auth.tunneling.disabledSchemes 的值被缓存到一个静态字段中。然后sun.net.www.protocol.http.HttpURLConnection 使用这个缓存值。

        结论:您必须在类加载器加载sun.net.www.protocol.http.HttpURLConnection(通过发出请求或其他方式)之前严格在代码中设置jdk.http.auth.tunneling.disabledSchemes。或将其设置在程序参数或jre/lib/net.properties 文件中。否则,更改jdk.http.auth.tunneling.disabledSchemes 的值无效。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多