2

I am getting the subject mentioned error code in one of my programs. I looked around for solutions on Stackoverflow and tried to follow the suggestions on issues reported by Ilana Platonov and PPr. Unfortunately, these didn't resolve the errors.

As a next step, I simply tried to run the code presented in Ilana Platonov along with the resolutions (accepted there). My code: package examples;

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

However, I still get the same errors that were reported by others:

   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)

What worked for others and not for me:

  1. The often given suggestion about setting the variables jdk.http.auth.tunneling.disabledSchemes and jdk.http.auth.proxying.disabledSchemes to blank did not work. I set these to blank in ..\Java\jdk1.8.0_112\jre\lib\net.properties file.

  2. Tying out with various JVM Args proposed by Andreas Panagiotidis didn't help either.

  3. From https://bugs.eclipse.org/bugs/show_bug.cgi?id=422665 I also tried to exclude HTTP libraries that might be conflicting. -Dorg.eclipse.ecf.provider.filetransfer.excludeContributors=org.eclipse.ecf.provider.filetransfer.httpclient4

I am using:

  • Eclipse IDE Photon Release (4.8.0),
  • java version "1.8.0_112" (build 1.8.0_112-b15)
  • Windows 10
  • I am behind a corporate proxy and have spent two full days on it without success. :(

Let me know if I should attach any other logs.

3 Answers 3

1

The corporate proxy was the culprit! Once I resolved those issues, the code that I have posted above worked smoothly.

Sign up to request clarification or add additional context in comments.

Comments

1

I came across another situation where I got the same error (with correct proxy details).

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

This time the error was due Java version 1.8.0_111. Since this Java update, the Basic Authentication for HTTPS tunneling has been disabled. This is done by adding the protocol to disabledScheme list, i.e., jdk.http.auth.tunneling.disabledSchemes=Basic in the lib/net.properties file.

To resolve this error enable the Basic Authentication by setting the variable jdk.http.auth.tunneling.disabledSchemes to emtpy string as: '-Djdk.http.auth.tunneling.disabledSchemes='

Enable/Disable Basic Authentication for HTTPS

Comments

1

I have just finished struggling with the same issue.

My application creates a HTTP transport instance and then uses it to make requests to the server. There is an option to configure this transport to use a proxy server. So there is a code in the transport's configuration method:

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

Looks OK, but all the requests through a proxy server failed with this Exception:

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

Then I noticed that my application makes a request to the server BEFORE creating the transport (and executing the lines above) to grab some data.

Now look at this line at the stack trace:

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

If you look at the source code of this class, you will find the static block, where the value of jdk.http.auth.tunneling.disabledSchemes is cached into a static field. And then sun.net.www.protocol.http.HttpURLConnection uses this cached value.

Conclusion: you must set jdk.http.auth.tunneling.disabledSchemes in your code strictly before the class loader loads sun.net.www.protocol.http.HttpURLConnection (by making request or some other way). Or set it in program arguments or jre/lib/net.properties file. Otherwise, changing the value of jdk.http.auth.tunneling.disabledSchemes has no effect.

1 Comment

THANK YOU! Finally I found an explanation for seemingly random behaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.