0
private static void setProxy(String proxyHostName,int proxyport){
    proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyHostName,proxyport));
}

private static void setProxy(String proxyHostName,int proxyport,String username,String password){
    setProxy(proxyHostName,proxyport);
    if (username!=null && password!=null) {
        Authenticator authenticator = new Authenticator() {

            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication(username, password.toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);
    }

}

this is code for proxy setting.I don't know why it's throwing this error.

exception:

java.io.IOException: Unable to tunnel through proxy. Proxy returns ""HTTP/1.1 407 Proxy Authentication Required"" at sun.net.http://www.protocol.http.httpurlconnection.dotunneling%28httpurlconnection.java:2142/) at sun.net.http://www.protocol.https.abstractdelegatehttpsurlconnection.connect%28abstractdelegatehttpsurlconnection.java:183/) at sun.net.http://www.protocol.https.httpsurlconnectionimpl.connect%28httpsurlconnectionimpl.java:162/) ..

CrazyCoder
  • 3
  • 1
  • 8

1 Answers1

0

Your code is incomplete and you did not specify which version of Java you are using, so I cannot say for certain, but I'm guessing this may be the reason: unable to tunnel through proxy since java-8-update-111.

Try modifying your setProxy() method like this:

private static void setProxy(String proxyHostName, int proxyport, String username, String password){
    setProxy(proxyHostName,proxyport);
    if (username!=null && password!=null) {
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
        Authenticator authenticator = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return (new PasswordAuthentication(username, password.toCharArray()));
            }
        };
        Authenticator.setDefault(authenticator);
    }
}

Alternatively run your app with -Djdk.http.auth.tunneling.disabledSchemes=.

More details on authenticated proxies in Java, can be found here: authenticated http proxy with java.

There's also an explanation of this behavior here: JDK-8210814

Konrad Botor
  • 3,904
  • 1
  • 10
  • 21