基于HttpClient的工具类HttpUtil
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
public class HttpUtil {
public static String doGet(String url) throws Exception {
HttpGet httpGet = new HttpGet(url);
return execute(httpGet);
}
public static String doPost(String url, Map<String, String> param) throws Exception {
HttpPost httpPost = new HttpPost(url);
ArrayList<BasicNameValuePair> arrayList = new ArrayList<BasicNameValuePair>();
Set<String> keySet = param.keySet();
for (String key : keySet) {
arrayList.add(new BasicNameValuePair(key, param.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
return execute(httpPost);
}
private static String execute(HttpRequestBase request) throws IOException, ClientProtocolException {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(request);
if (200 == response.getStatusLine().getStatusCode()) {
return EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
} else {
System.out.println(EntityUtils.toString(response.getEntity(), Charset.forName("utf-8")));
}
return "";
}
}
更多信息:
使用代理IP地址开发某网站自动投票程序
使用代理IP编写Java网络爬虫
网络爬虫攻防常见技巧
个人网站:shuoyizui.com
公众号:写个框架玩
近期在公众号会发布一系列文章,主要是想完成一个简化的MapReduce框架的编写。实现Mapreduce编程模型、任务远程提交、任务分配、任务执行等功能。设计到了动态代理、反射、网络通信、序列化、消息队列、netty、自定义类加载器、多线程、shell等技术点。