之前的项目一直在使用Commons的HttpClient来作为网络连接工具使用,现在闲来无事想自己写一个爬虫项目,发现这玩意都已经更新到4.0以上了,并且从Commons项目中脱离出来了。往上查了查文档,各式各样的教程看的眼花缭乱,借助着百度翻译(渣渣英语)从新给自己写一个HttpClient4.X的学习笔记吧(参考Apache Software 中项目4.5的官方API来学习)
一. 基础
1.1 执行请求
简单的Demo
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
<...>
} finally {
response.close();
}
1.1.1 HttpRequest
定义请求在他给出的api中给出了两种
HttpGet httpget = new HttpGet("http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.google.com")
.setPath("/search")
.setParameter("q", "httpclient")
.setParameter("btnG", "Google Search")
.setParameter("aq", "f")
.setParameter("oq", "")
.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());