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.
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.
Finally I figured it out.
Added Authorization Header to the request.
String credential = Credentials.basic(proxyUsername, proxyPassword);
urlConnection.addRequestProperty("Proxy-Authorization", credential);