专家回答:
在Apache HttpClient 4.1.3中,要设置nonProxyHosts,您需要使用HttpClientBuilder
构建一个HttpClient
对象,并使用HttpHost
和ProxySelector
进行设置。
具体步骤如下:
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
HttpClientBuilder
对象,并设置代理服务器CloseableHttpClient httpclient = HttpClients.custom()
.setProxy(new HttpHost("proxy_host_url", proxy_port))
.build();
其中,proxy_host_url
为代理服务器的URL,proxy_port
为代理服务器的端口。
HttpGet
对象,并使用HttpClientBuilder
对象设置请求参数HttpGet httpget = new HttpGet("http://www.example.com");
httpget.setConfig(requestConfig);
其中,requestConfig
为请求配置对象,可以包含诸如请求方法、请求头、请求正文等参数。
CloseableHttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
response.close();
httpclient.close();
以上是使用Apache HttpClient 4.1.3设置nonProxyHosts的示例代码。
领取专属 10元无门槛券
手把手带您无忧上云