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;and put these lines of code before your code opens an URLConnection-
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());
}
}
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.

9 Comments:
Hi
This works fine in case of stand alone program. But in case of a web application it is throwing
Proxy authentication requires error.
Is there any thing to change in the code, when run as a web application.
I am using weblogic 9.2 to run my application.
Can you please help me ijn this case.
Thanks in Advance
Praveen
Hi Praveen,
No Idea about WL 9.2, though it works fine on Tomcat.
Thanks,
Vinod
i tried this. but i m getting following exception
can u help me out ????
*************************************************************
11:44:16,688 INFO [STDOUT] 2008-10-30 11:44:16,688 ERROR [HttpMethodDirector-authenticate-235]-Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials
org.apache.commons.httpclient.auth.InvalidCredentialsException: Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials
11:44:16,719 INFO [STDOUT] 2008-10-30 11:44:16,719 ERROR [searchTrip_jsp-_jspService-658]-Transport error: 407 Error: Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
org.apache.axis2.AxisFault: Transport error: 407 Error: Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
***************************************************************
This example uses System properties. This is not ideal for web applications--as this system property will affect the complete JVM running the web application.
We ran into issues when we deployed two web-applications, one of which connected to a local HTTP server, and the other using proxy connected to a internet site. When we set the system properties, the one requiring local connection was also trying to go through the proxy.
Finally we used Apache HTTP Client:
http://hc.apache.org/httpcomponents-client/index.html
This has a neat proxy authentication implementation.
subWiz,
Thanks, for the update.
thanks Mr.vinod,i tried this and it worked properly.......
Would this work for ftp.proxy* as well?
I think it should, though I never tried it for ftp protocol hence no definitive answer ;-)
is there a way , where you can set proxyUser, and proxyPassword, without setting property in System class.
Because i have this issue on Weblogic, not able to set property in system class(i mean the system.setProperty has no effect)
I tried apach HttpClient as well, it doesnot support NTLM.
Tejas
Post a Comment