使用HttpProxy通过preemtive身份验证连接到主机 [英] Using HttpProxy to connect to a host with preemtive authentication
问题描述
我正在使用HttpClient连接到需要BasicAUTH的主机。但代理不需要任何身份验证。我已将其设置如下:
I am using HttpClient to connect to a host which requires BasicAUTH. But the proxy doesn't require any authentication. I have set it up as follows:
private final HttpClient httpClient; // Spring injected
设置基本身份验证:
private void setBasicAuth(final String username, final String password) {
httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
httpClient.getParams().setAuthenticationPreemptive(true);
}
设置代理:
private void setProxy(final String proxyHost, final int proxyPort) {
hostConfiguration hostConfiguration = httpClient.getHostConfiguration();
hostConfiguration.setProxy(proxyHost, proxyPort);
}
但是在运行代码时我收到以下警告。一切正常,但我也想摆脱警告(或者至少理解它们出现的原因)
But I get the following warnings when running the code. Everything works, but I want to get rid of the warnings as well (or at least understand why they appears)
WARN o.a.c.httpclient.HttpMethodDirector - Required proxy credentials not available for BASIC <any realm>@proxy.XXXXXX.no:3128
WARN o.a.c.httpclient.HttpMethodDirector - Preemptive authentication requested but no default proxy credentials available
任何想法?
推荐答案
这里是来自Apache站点的代理没有凭证的示例:
Here's an example from the Apache site for a proxy w/o credentials:
(来自 http://hc.apache.org/httpcomponents-cl ient-ga / examples.html )
您收到错误是因为您传递了用户名/密码而且不需要。
You are getting the error because you are passing in a username/password and don't need to.
WARN消息来自记录器(http://hc.apache.org/httpcomponents-client-ga/logging.html) - 取决于你的记录器的方式设置你可能只是忽略它。
The WARN messages are coming from the logger (http://hc.apache.org/httpcomponents-client-ga/logging.html) - depending on how you have your logger set up you could just ignore that.
花了太多时间处理尝试使用代理服务器处理Java应用程序,我可以告诉您使用Proxifier等工具( http://www.proxifier.com/ for Mac OS X和Windows)或CNTLM( http://cntlm.sourceforge.net/ )更容易,更灵活,更容易调试,并保持代码清洁。
Having spent WAY too much time dealing with trying to make a Java application deal with proxy servers, I can tell you that using a tool such as Proxifier ( http://www.proxifier.com/ for Mac OS X and Windows) or CNTLM ( http://cntlm.sourceforge.net/) was much easier, more flexible, easier to debug, and kept the code clean.
这篇关于使用HttpProxy通过preemtive身份验证连接到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!