有时候由于不可抗力,我们爬外面的东西的时候需要设置代理,设置方法如下:
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);