Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient 4.1 - Proxy Authentication

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?

like image 805
Daniel C. Sobral Avatar asked Aug 05 '11 20:08

Daniel C. Sobral


People also ask

What is closeable HttpClient?

CloseableHttpClient is the base class of the httpclient library, the one all implementations use. Other subclasses are for the most part deprecated. The HttpClient is an interface for this class and other classes. You should then use the CloseableHttpClient in your code, and create it using the HttpClientBuilder .

What is Apache HttpClient?

HttpClient is a HTTP/1.1 compliant HTTP agent implementation based on HttpCore. It also provides reusable components for client-side authentication, HTTP state management, and HTTP connection management.


1 Answers

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:

NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName );  CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds ); HttpClientBuilder clientBuilder = HttpClientBuilder.create();  clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort())); clientBuilder.setDefaultCredentialsProvider(credsProvider); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());  CloseableHttpClient client = clientBuilder.build(); 
like image 198
Hotel Avatar answered Sep 25 '22 12:09

Hotel