-1

I am sending a post request with following code.

    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyAddr, proxyPort));
    URLConnection urlConnection = url.openConnection(proxy);
    urlConnection.setRequestMethod("POST");
    urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    ...

Here I can set the proxy server address and port. But I wonder how I can add proxy username and password here.

I read some posts about calling http request via proxy but no one was talking about proxy authentication.

Hope some help from you guys.


PS:

Some people suggest using this code from here

Authenticator authenticator = new Authenticator() {

    public PasswordAuthentication getPasswordAuthentication() {
        return (new PasswordAuthentication("user",
                "password".toCharArray()));
    }
};
Authenticator.setDefault(authenticator);

but this is not working at all.

Community
  • 1
  • 1
ariane26
  • 163
  • 1
  • 6
  • 25

1 Answers1

0

Finally I figured it out.

Added Authorization Header to the request.

String credential = Credentials.basic(proxyUsername, proxyPassword);
urlConnection.addRequestProperty("Proxy-Authorization", credential);
ariane26
  • 163
  • 1
  • 6
  • 25