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-
Now all calls will successfully pass through the proxy authentication.
see the full browser support code here
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_iiitm", "password123")); System.setProperty("http.proxyHost", "proxy host"); System.setProperty("http.proxyPort", "port");
Now all calls will successfully pass through the proxy authentication.
see the full browser support code here
No comments:
Post a Comment