2

我在我的一个程序中收到了主题提到的错误代码。我在 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)
  • 视窗 10
  • 我在公司代理背后,花了整整两天时间没有成功。:(

让我知道我是否应该附上任何其他日志。

4

3 回答 3

1

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

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='

启用/禁用 HTTPS 的基本身份验证

于 2018-11-05T13:31:50.360 回答
1

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

于 2018-10-04T15:57:17.567 回答
0

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

我的应用程序创建一个 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使用这个缓存的值。

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

于 2022-02-19T01:18:54.607 回答