1

I am using Apache HttpClient library to connect to url. The network in which i am doing has a secure proxy to it. when i am using the java.net package to connect to the url i just have to add the

System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort); 

no proxy userid and password is needed to be passed but when i am trying to connect through httpclient i am getting 407 proxy authentication error.My code is:

HttpHost proxy = new HttpHost("xyz.abc.com",8080,"http");
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

Proxy is using NTML authentication.I don't want to pass userid and password.

Vaibhav Jain
  • 119
  • 3
  • 16
  • The nearest answer i find is on stackoverflow : http://stackoverflow.com/questions/6962047/apache-httpclient-4-1-proxy-authentication I just think that NTLM credentials of your windows system are not accessible from your java layer and then you need to reauthenticate to the proxy. I might be wrong. – philippe lhardy Dec 19 '12 at 19:41
  • First of all i think the out of box NTML support is not there in HttpClient,Secondly the credential changes quite frequently so i can't use in that way. accessing credentials of my windows system through java layer if it available to java.net package URLConnect so it would be available for httpclient library also. – Vaibhav Jain Dec 19 '12 at 19:54

2 Answers2

1

I have upgraded to httpclient 4.2 and this version has out of box NTML support. Just need to add following lines to the code

HttpClient httpclient = new DefaultHttpClient();
NTCredentials creds = new NTCredentials("user", "pwd", "myworkstation", "microsoft.com");
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

For further reading Httpclent authentication scheme u can refer http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html#d5e947

But my question is still open, why HttpClent is not picking the system proxy as simple java program does.

Vaibhav Jain
  • 119
  • 3
  • 16
1

In order for the system properties to be picked up, you could use SystemDefaultHttpClient instead of DefaultHttpClient.

As of HttpClient 4.3, this class has been deprecated in favor of HttpClientBuilder:

HttpClient hc = new HttpClientBuilder().useSystemProperties().build();
Jon Ekdahl
  • 382
  • 1
  • 3
  • 11