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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容