Question

System.setProperty("http.proxySet", "true");
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("http.proxyHost", "192.168.1.103");
System.setProperty("http.proxyPort", "3128");
System.setProperty("http.proxyUser", "user123");
System.setProperty("http.proxyPassword", "passwD123");

url = new URL("http://www.google.co.in");

every time when I am using this code IOException throws which say HTTP response code 407. HTTP 407 means proxy authentication required. why this problem is coming while I set proxyUser and proxyPassword. enter image description here
http 401 will occur if I put wrong password but it always give me 407, means my code does not take username and password. In above code user123 is username and passwD123 is password for proxy authentication.

Was it helpful?

Solution

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html

I found the solution thanks Mr. Vinod Singh.

Proxy authentication in Java

The usual corporate networks provide internet access via proxy servers and at times they require authentication as well. May applications do open the connections to servers which are external to the corporate intranet. So one has to do proxy authentication programmatically. Fortunately Java provides a transparent mechanism to do proxy authentications.

Create a simple class like below-

import java.net.Authenticator;

class ProxyAuthenticator extends Authenticator {

    private String user, password;

    public ProxyAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, password.toCharArray());
    }
}

and put these lines of code before your code opens an URLConnection-

Authenticator.setDefault(new ProxyAuthenticator("user", "password"));
System.setProperty("http.proxyHost", "proxy host");
System.setProperty("http.proxyPort", "port");

Now all calls will successfully pass through the proxy authentication.

OTHER TIPS

@GauravDS You mentioned:

http://blog.vinodsingh.com/2008/05/proxy-authentication-in-java.html I found the solution thanks Mr. Vinod Singh. Proxy authentication in Java The usual corporate networks provide internet access via proxy servers and at times they require authentication as well. May applications do open the connections to servers which are external to the corporate intranet. So one has to do proxy authentication programmatically. Fortunately Java provides a transparent mechanism to do proxy authentications. Create a simple class like below- .
.
.
and put these lines of code before your code opens an URLConnection- Authenticator.setDefault(new ProxyAuthenticator("user", "password")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port"); Now all calls will successfully pass through the proxy authentication.

What if the site you are connecting to also requires a username/password to allow you. Setting a Default Authenticator(Authenticator.setDefault) will fail I guess when the external site will look for authenticated user.

Any views?....Someone ?

Edit:1 Used this code earlier and was getting the error (407) Proxy Authentication Required. I believe that was because the authentication was requested by different hosts. and when you set a default authenticator with one user/pass for one host, then the authentication will fail for other requesting host. I made the following change yesterday to SimpleAuthenticator class and now it works like a charm.

   protected PasswordAuthentication getPasswordAuthentication()
   {
    String requestingHost = getRequestingHost();
    if (requestingHost == proxyHost){
        System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost );
        return new PasswordAuthentication(proxyuser,proxypass.toCharArray());
    }
    else{
        System.out.println("getPasswordAuthentication() request recieved from->" + requestingHost );
        return new PasswordAuthentication(sharepointusername,sharepointpassword.toCharArray());
    }

   }

More info here: http://blog.ashwani.co.in/blog/2013-07-29/access-sharepoint-webservices-from-java-behind-proxy/

The answer to use an Authenticator is correct for the general case. However, another cause of HTTP 407 in Java 8u111 and later is if you are using BASIC authentication against the proxy.

In this case, add this system property:

-Djdk.http.auth.tunneling.disabledSchemes=

I found this out from: https://confluence.atlassian.com/kb/basic-authentication-fails-for-outgoing-proxy-in-java-8u111-909643110.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top