How can I configure the username and password to authenticate a http proxy server using Java?
I just found the following configuration parameters:
http.proxyHost=<proxyAddress>
http.proxyPort=<proxyPort>
https.proxyHost=<proxyAddress>
https.proxyPort=<proxyPort>
But, my proxy server requires authentication. How can I configure my app to use the proxy server?
Try this runner I wrote. It could be helpful.
(EDIT: As pointed out by the OP, the using a
java.net.Authenticator
is required too. I'm updating my answer accordingly for the sake of correctness.)For authentication, use
java.net.Authenticator
to set proxy's configuration and set the system propertieshttp.proxyUser
andhttp.proxyPassword
.You're almost there, you just have to append:
Most of the answer are there, but for me not quite. This is what works for me with java.net.HttpURLConnection (I have tested all the cases with JDK 7 and JDK 8). Note that you do not have to use the Authenticator class.
Case 1 : Proxy without user authentication, access HTTP resources
Case 2 : Proxy with user authentication, access HTTP resources
Case 3 : Proxy without user authentication, access HTTPS resources (SSL)
Case 4 : Proxy with user authentication, access HTTPS resources (SSL)
Case 5 : Proxy without user authentication, access both HTTP and HTTPS resources (SSL)
Case 6 : Proxy with user authentication, access both HTTP and HTTPS resources (SSL)
You can set the properties in the with System.setProperty("key", "value) too.
To access HTTPS resource you may have to trust the resource by downloading the server certificate and saving it in a trust store and then using that trust store. ie
For Java 1.8 and higher you must set
to make proxies with Basic Authorization working with https along with Authenticator as mentioned in accepted answer
But, setting only that parameters, the authentication don't works.
Are necessary to add to that code the following: