Java communication fails through web proxy using Apache HttpClient

空扰寡人 提交于 2019-11-30 14:43:37

I'll take a shot. If the proxy server is using basic authentication, you could use the following snippet as an example:

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 

If the proxy server is using NTLM authentication, I do not believe NTLM support is available for all proxy server versions (it will work with some NTLM authentication proxy server versions - check if the proxy is using v1 or v2 of NTLM authentication). For reference & workarounds, you can check the following:

http://hc.apache.org/httpcomponents-client-ga/ntlm.html

http://htmlunit.sourceforge.net/ntlm.html

http://devsac.blogspot.com/2010/10/supoprt-for-ntlmv2-with-apache.html

Depending on your proxy server, you may need to look into NTCredentials instead of UserPasswordCredentials.

I would also suggest that you may wish to look into using wireshark to capture network packets and check the response from the proxy server to be absolutely sure what is causing the problem.

Just for completeness, since this question is found searching for JVM http.proxy properties, if using JVM infrastructure proxy username and passwords can be provided using :

System.setProperty("http.proxyUser",proxyUserName)
System.setProperty("http.proxyPassword",proxyUsePassword).

For some reason, setting parameters on client didn't work for me.

But worked on http method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!