Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top