I've been trying to configure the user and password for proxy authentication from the configured properties while using Apaches HttpComponent's httpclient, but with no success. All examples I have found refer to methods and classes that are no longer available, such as HttpState
and setProxyCredentials
.
So, can anyone give me an example of how to configure the proxy credentials?
For anyone looking for the answer for 4.3...its fairly new and their example didn't use the new HttpClientBuilder...so this is how I implemented this in that version:
How to setup proxy authentication using Apache's httpclient
(Pre-authorization on proxy networks)
This answer uses Apache's HttpClient v4.1 and later.
The accepted answer didn't work for me, but I found something else that did!
Here's some tested, verified code from apache that demonstrates how to authenticate through a proxy for a HTTP request.
The full documentation is located here: https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html .
There's also an excellent example from Apache here: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java
my_username
with your proxy usernamemy_password
with your proxy passwordproxy.mycompany.com
with your proxy host8080
with your proxy portgoogle.com
with the host of the site that you want to send your HTTP request to./some-path
with the path that you want to send the HTTP request to. This uses the host site you specified earlier (google.com).The following example will authenticate
username:password@proxy.mycompany.com:8080
and send aGET
request tohttp://www.google.com/some-path
and will print the response HTTP code.For HttpClient 4.5 and per request authentication:
A simpler thing worked for me for NTLM:
If you have to keep your code working with 4.1 or want to use the below snippet, it's important to know that httpclient 4.1 will not send the authentication to proxy. You will probably get a 407 "Proxy Authentication Required" status code. I upgraded to 4.3.3 and all worked well, although DefaultHttpClient and ConnRoutePNames were deprecated in this release. Hope this helps!
For Basic-Auth it looks like this:
AFAIK NTLM is not supported out of the box. But you might be able to manage that using
NTCredentials
and maybe overloadingDefaultProxyAuthenticationHandler
.