1.前言
http协议可以说是现在Internet上面最重要,使用最多的协议之一了,越来越多的java应用需要使用http协议来访问网络资源,特别是现在rest api的流行,HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient,很多的java的爬虫也是通过HttpClient实现的,研究HttpClient对我们来说是非常重要的。
HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。因此熟练掌握HttpClient是很重要的必修内容,掌握HttpClient后,相信对于Http协议的了解会更加深入。
2.什么是httpClient
很多人觉得既然HttpClient是一个HTTP客户端编程工具,很多人把他当做浏览器来理解,但是其实HttpClient不是浏览器,它是一个HTTP通信库,因此它只提供一个通用浏览器应用程序所期望的功能子集,最根本的区别是HttpClient中没有用户界面,浏览器需要一个渲染引擎来显示页面,并解释用户输入,例如鼠标点击显示页面上的某处,有一个布局引擎,计算如何显示HTML页面,包括级联样式表和图像。javascript解释器运行嵌入HTML页面或从HTML页面引用的javascript代码。来自用户界面的事件被传递到javascript解释器进行处理。除此之外,还有用于插件的接口,可以处理Applet,嵌入式媒体对象(如pdf文件,Quicktime电影和Flash动画)或ActiveX控件(可以执行任何操作)。HttpClient只能以编程的方式通过其API用于传输和接受HTTP消息。HttpClient也是完全内容不可知的。
3.使用方法
使用HttpClient发送请求、接收响应很简单,一般需要如下几步:
- 创建HttpClient对象。
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
- 释放连接。无论执行方法是否成功,都必须释放连接。
4.HttpClient入门使用
//httpclient的接口基本都在这儿
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
//httpclient缓存
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5</version>
</dependency>
//http的mime类型都在这里面
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.2</version>
</dependency>
1) 抓取网页的内容并打印到控制台的demo
package com.example.demoo.util;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.*;
/**
* 抓取网页的内容并打印到控制台的demo
* Created by lanxinghua on 2018/7/26.
*/
public class HttpGetDemo {
public static void main(String[] args) {
String url="http://www.baidu.com";
//1.获取httpclient
CloseableHttpClient client = HttpClients.createDefault();
//2.使用get方法
HttpGet httpGet = new HttpGet(url);
InputStream inputStream = null;
CloseableHttpResponse response = null;
try {
//3.执行请求,获取响应
response = client.execute(httpGet);
//4.获取响应数据
System.out.println("响应状态码:"+response.getStatusLine().getStatusCode());
//获取响应实体内容
HttpEntity entity = response.getEntity();
//方法一
/*if (entity!=null){
System.out.println(EntityUtils.toString(entity));
}
EntityUtils.consume(entity);*/
//方法二
if (entity!=null){
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line="";
while ((line = reader.readLine())!=null){
System.out.println(line);
}
}
//5.释放连接
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2) post请求
package com.example.demoo.util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 最简单的post请求
* Created by lanxinghua on 2018/7/26.
*/
public class HttpPostDemo {
public static void main(String[] args) throws IOException {
//表单参数
List<NameValuePair> formparam = new ArrayList<>();
formparam.add(new BasicNameValuePair("account","陈星星"));
formparam.add(new BasicNameValuePair("password","123456"));
HttpEntity reqEntity = new UrlEncodedFormEntity(formparam,"utf-8");
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000)//连接超时
.setSocketTimeout(5000)//读取超时
.setConnectionRequestTimeout(5000)
.build();
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("http://cnivi.com.cn/login");
post.setEntity(reqEntity);
post.setConfig(config);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String msg = EntityUtils.toString(entity,"utf-8");
System.out.println(msg);
}else {
System.out.println("请求失败!");
}
}
}
5.Httpclient总结
1) 如果有请求参数的话,Get方法直接写在url后面,例如
HttpGet httpget = new HttpGet(
“http://www.google.com/search?hl=zh-CN&q=httpclient&btnG=Google+Search&aq=f&oq=”);
或者使用setParameter来设置参数
URI uri = new URIBuilder()
.setScheme(“http”)
.setHost(“www.google.com”)
.setPath(“/ search”)
.setParameter(“q”,“httpclient”)
.setParameter(“btnG”,“Google搜索”)
.setParameter(“aq”,“f”)
.setParameter(“oq”,“”)
。建立();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
post方法用setEntity(HttpEntity entity)方法来设置请求参数。
2) 获取请求结果
HttpEntity entity = response.getEntity();
//方法一
if(entity!=null)
{
System.out.println(EntityUtils.toString(entity,”utf-8”));
}
EntityUtils.consume(entity)
//方法二
InputStream inputStream = entity.getContent();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = “”;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}