在爬取数据的时候有的时候不能使用自己的真实ip爬取,需要简单配置一下用代理来完成。
public static String getHttpGet(String url){ //get请求
HttpHost proxy = new HttpHost("127.0.0.1", 1080);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
HttpGet get=new HttpGet(url);
try {
CloseableHttpResponse response = httpclient.execute(get);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
本文介绍了一种通过设置代理服务器进行HTTP GET请求的方法,以此实现网页数据的爬取。文章详细展示了如何利用Java编程语言及相关的HTTP客户端库,通过指定本地代理服务器地址和端口来发起GET请求,并获取目标网址的内容。

767

被折叠的 条评论
为什么被折叠?



