Apache HttpClient 4.1 - Proxy Authentication

2020-01-27 03:23发布

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?

7条回答
看我几分像从前
2楼-- · 2020-01-27 03:50

Instead of NTLM one can use just plain old username and password on 4.3+ httpClient, as follows:

HttpHost proxy = new HttpHost("x.x.com",8080);
Credentials credentials = new UsernamePasswordCredentials("username","password");
AuthScope authScope = new AuthScope("x.x.com", 8080);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope, credentials);
HttpClient client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
HttpResponse response=client.execute(new HttpGet("http://stackoverflow.com/questions/6962047/apache-httpclient-4-1-proxy-authentication"));
查看更多
登录 后发表回答