http 407 proxy authentication required : how to handle in java code
Asked Answered
F

3

20
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.

Fuel answered 1/1, 2013 at 19:6 Comment(2)
What http client are you using?Erst
I am trying to make manual proxy, automatic proxy , no proxy support browser in java. Browser like firefox supports this, Tools->option->Advanced->Network->setting.Fuel
F
23

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.

Fuel answered 1/1, 2013 at 20:57 Comment(1)
I have setup http and https proxy in run configuration on eclipse with user and password , for some reason it works intermittently which is very strange.Ihab
M
10

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

Mika answered 1/12, 2017 at 9:24 Comment(0)
F
8

@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.

Used this code earlier and I was still getting the error (407) Proxy Authentication Required.

What if the site you are connecting to also requires a username/password to allow you? Setting a Default Authenticator(Authenticator.setDefault) fails when the external site looks for its own authenticated user.

I made the following change yesterday to the 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/

Fleshpots answered 29/7, 2013 at 16:8 Comment(5)
what http response for the site authentication ?? above mechanism for the proxy server in corporate/ institutes network.Fuel
Edited my response above.Fleshpots
You could check for getRequestorType() == RequestorType.PROXY too. Authentication that is mean to credintial or login on applications normally use 401 and proxies use 407.Sheedy
I have setup http and https proxy in run configuration on eclipse with user and password , for some reason it works intermittently which is very strange.Ihab
I think you want requestingHost.equals(proxyHost) on the fourth line. It may sometimes work with ==, depending on string caching mechanisms, but is not reliable.Rugby

© 2022 - 2024 — McMap. All rights reserved.