Jsoup和HttpClient4.3设置代理爬内容

有时候由于不可抗力,我们爬外面的东西的时候需要设置代理,设置方法如下:

Jsoup

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
Connection connection = Jsoup.connect(url).proxy(proxy);

HttpClient4.3

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet(moreUrl);
HttpHost proxy = new HttpHost("localhost", 1080);
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
get.setConfig(config);
CloseableHttpResponse response = httpclient.execute(get);

参考:
https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java

你可能感兴趣的:(Jsoup和HttpClient4.3设置代理爬内容)