【问题标题】:httpClient proxy support in apache commons 3.1apache commons 3.1 中的 httpClient 代理支持
【发布时间】:2012-11-02 16:03:53
【问题描述】:

我正在使用 apache commons 3.1 来实现带有代理支持的 httpClient。 我正在尝试通过代理连接到远程主机。代理服务器配置为没有任何身份验证,但是远程主机配置了身份验证。 当我通过属性文件传递代理参数时,它会在执行时发出警告:

警告 - 所需的代理凭据不适用于 BASIC @xx.xx.xx.xx 警告 - 请求抢先身份验证,但没有可用的默认代理凭据

但执行仍在继续。

另一方面,当我通过 JVM 参数传递代理参数时,再次给出相同的警告并停止执行。

这种行为有什么具体原因吗?通过属性文件和JVM arg传递代理参数有什么区别吗?

代码如下:

if(System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) {
            httpClient.getHostConfiguration().setProxy(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")));
        }
        else if(AMXAdminTask.props.getProperty("http.proxyHost") != null && AMXAdminTask.props.getProperty("http.proxyPort") != null) {
            httpClient.getHostConfiguration().setProxy(Propfile.props.getProperty("http.proxyHost"), Integer.parseInt(Propfile.props.getProperty("http.proxyPort")));
        }

【问题讨论】:

    标签: java apache proxy apache-commons


    【解决方案1】:

    看起来您正在尝试将两种截然不同的事物结合起来。您在上面发布的代码正确地让您通过您的代理,但远程主机需要 BASIC 身份验证。下面的示例使用 Jersey 客户端(在现有项目中用于进行 RESTful 调用),但您应该了解您需要做什么。如果您坚持使用 Apache HttpComponents,请查看以下内容: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

    import org.apache.commons.lang.StringUtils;
    
    import com.sun.jersey.api.client.Client;
    import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
    import com.sun.jersey.client.apache.ApacheHttpClient;
    import com.sun.jersey.client.apache.config.ApacheHttpClientConfig;
    import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
    
    public abstract class BaseProxyProvider {
        protected Client getHttpClient() {
            final DefaultApacheHttpClientConfig cc = new DefaultApacheHttpClientConfig();
            if (StringUtils.isNotEmpty(System.getProperty("http.proxyHost"))) {
                cc.getProperties()
                        .put(ApacheHttpClientConfig.PROPERTY_PROXY_URI,
                                "http://" + System.getProperty("http.proxyHost") + ":"
                                        + System.getProperty("http.proxyPort") + "/");
            }
            Client c = ApacheHttpClient.create(cc);
    
            c.addFilter(new HTTPBasicAuthFilter(WebAppPropertyReader.getProperties().getProperty(
                    WebAppPropertyReader.SERVICE_USER), WebAppPropertyReader.getProperties().getProperty(
                    WebAppPropertyReader.SERVICE_PASSWORD)));
            return c;
        }
    }
    

    【讨论】:

    • 我可以用 fiddler 用相同的代码测试代理服务器。我可以从属性文件和 JVM ags 中传递代理参数。
    • 现在我遇到了关于代理服务器身份验证的问题。我使用提琴手作为代理服务器。我在代理服务器和远程主机都进行了身份验证。我在 httpClient 标头中设置了 http 代理凭据。但我在响应中收到错误:“传输错误 407:需要代理身份验证。”在提琴手中,我看到一条消息:“不存在 WWW-Authenticate Header。”
    • 您需要为您的代理用户和密码设置系统属性:System.setProperty("http.proxyUser", authUser);System.setProperty("http.proxyPassword", authPassword);
    • @DomenicD。这可能不是在所有情况下都需要的。 HttpClient 允许您在代码中指定代理的凭据。
    猜你喜欢
    • 2011-02-16
    • 2017-03-22
    • 2015-01-04
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多