Java使用HTTP客户端4.5客户端获取语句

时间:2015-06-19 16:35:47

标签: java apache httpclient

我正在尝试将代码从Apache HTTP客户端3.1更新到4.5,我有几种方法,如client.getHostConfigurationclient.getStateclient.getHttpConnectionManager。所有这些都不适用于较新版本的httpclient,所以我想知道如何重写这些。我在HTTP客户端文档中看到的只是getParams。但我不知道如何从中获取所有其他信息。

如果有人想要在

中使用这些内容
if(getProxy() != null) {
        client.getHostConfiguration().setProxy(getProxy().getHost(),getProxy().getPort());
        if (HttpProxyCredentials.isProxySet()) {
            AuthScope authScope = new AuthScope(getProxy().getHost(), getProxy().getPort()); 
            client.getState().setProxyCredentials(authScope, new NTCredentials(HttpProxyCredentials.getUserName(),
                    HttpProxyCredentials.getPassword(),
                    "",HttpProxyCredentials.getDomain())); 

1 个答案:

答案 0 :(得分:1)

这是构建方法的现代代理示例:

RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setCookieSpec(CookieSpecs.BEST_MATCH)
                .setExpectContinueEnabled(true)
                .setStaleConnectionCheckEnabled(true).setSocketTimeout(timeout)
                .build();

        if (proxyHost != null) {
            defaultRequestConfig = RequestConfig.copy(defaultRequestConfig)
                    .setProxy(new HttpHost(proxyHost, proxyPort)).build();
        }

        method.setConfig(defaultRequestConfig);