0

I'm using java Proxy class and passing it to HttpURLConnection.openConnection().
Is there a way to provide authentication information (just like http.proxyUser and http.proxyPassword) to the Proxy class?
Thanks

1 Answer 1

2

You can use Authenticator for that purpose:

Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("login", "password".toCharArray());
        }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Will it affect the whole communication or just the relevant connection I'm opening?
@danieln Authenticator is static, so it is global to your application. But it will be invoked only if necessary. If you opened other connections that donnot require authentication, it won't be invoked.
So it is global to my application but not to the entire application server? (like http.proxyUser). And what if I want different authentications for different connections?
@danieln inside the method getPasswordAuthentication() you can interrogate the Authenticator to find out for which request authentication is requested (getRequestingURL(), etc...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.