Java–HttpClient
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
通过HttpClient可以模拟发出http请求,支持各种请求如get\post\delete等http协议。
也可以做爬虫。
使用Get请求测试一下:
HttpGet请求
通过Get请求获取到响应回来的响应体数据
public void test() throws Exception{
//创建一个客户端
CloseableHttpClient client = HttpClients.createDefault();
//创建一个Get请求
HttpGet httpGet = new HttpGet("http://jsonplaceholder.typicode.com/users");
//通过客户端执行该请求,返回一个响应
CloseableHttpResponse response = client.execute(httpGet);
//获取响应体对象
HttpEntity entity = response.getEntity();
//获取响应体内容
InputStream content = entity.getContent();
byte[] bytes=new byte[102400];
int read = content.read(bytes);
//输出到控制台
System.out.println(new String(bytes,0,read));
//关闭掉所有流
content.close();
response.close();
client.close();
}
//响应回来的Json数据
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
各种请求都是继承自HttpRequestBase,HttpRequestBase再向上继承。
基本上只是方法名字不同。
@NotThreadSafe
public class HttpGet extends HttpRequestBase {
public final static String METHOD_NAME = "GET";
public HttpGet() {
super();
}
public HttpGet(final URI uri) {
super();
setURI(uri);
}
/**
* @throws IllegalArgumentException if the uri is invalid.
*/
public HttpGet(final String uri) {
super();
setURI(URI.create(uri));
}
@Override
public String getMethod() {
return METHOD_NAME;
}
}
爬虫
HttpClient也可以用于爬虫,如:
将获取的网络资源输出到文件中。
//传入url,以及资源路径
public static void getUrl(String url,String path) throws Exception{
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
//打印流
PrintStream printStream=new PrintStream(path);
int len;
while((len=inputStream.read())!=-1){
printStream.write(len);
}
inputStream.close();
printStream.close();
response.close();
client.close();
}
@Test
public void test2() throws Exception{
getUrl("http://jsonplaceholder.typicode.com/users","./get.json");
}