HttpClient入门(1) 发送请求,处理响应及响应重复读取
HttpClient是Apache旗下的项目,是一个负责创建和维护HTTP和相关协议的工具集。
以下分析使用版本为:
httpclient-4.5.3.jar
httpcore-4.4.6.jar
jdk1.8.0_131
所有示例代码均经过运行测试
发送请求
httpclient最重要的功能就是发送http请求,下面介绍如何执行一个get请求:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
httpclient支持Http/1.1规范中定义的所有方法:GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,对应的类是:HttpGet,HttpHead,HttpPost,HttpPut,HttpDelete,HttpTrace,HttpOptions。
请求URI
URI(统一资源标识符),用于标识应用请求的资源,通常由协议版本,主机名,端口(可选),资源路径,参数名(可选),参数值(可选)组成。
请求时,可以通过拼接字符串的形式访问:
HttpGet get = new HttpGet("http://www.jianshu.com/p/7021031d6e49?utm_medium=index-banner&utm_source=desktop");
httpclient也提供了URIBuilder这个类简化请求URI的创建和修改。
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.jianshu.com")
.setPath("/p/7021031d6e49")
.setParameter("utm_medium", "index-banner")
.setParameter("utm_source", "desktop")
.build();
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
输出
http://www.jianshu.com/p/7021031d6e49?utm_medium=index-banner&utm_source=desktop
http请求是客户端发给服务端的一个消息,消息的第一行包括了请求方法,URI以及使用的协议版本。
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
RequestLine requestLine = get.getRequestLine();
System.out.println(requestLine.getMethod());
System.out.println(requestLine.getUri());
System.out.println(requestLine.getProtocolVersion());
System.out.println(requestLine);
输出
GET
http://www.jianshu.com/u/8a3115bb299c
HTTP/1.1
GET http://www.jianshu.com/u/8a3115bb299c HTTP/1.1
处理响应
服务端接收并处理请求后,会返回消息给到客户端,该消息的第一行包括协议版本,状态码以及描述文字。
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
System.out.println(response.getStatusLine());
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
System.out.println(response.getStatusLine().toString());
} catch (Exception e) {
e.printStackTrace();
}
输出
HTTP/1.1 200 OK
HTTP/1.1
200
OK
HTTP/1.1 200 OK
服务端返回的响应被封装在HttpEntity,通过HttpEntity可以获取请求响应的一些元信息,比如Content-Type,Content-Length以及Content-Encoding,元信息需由服务端提供,否则将是空值。
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
HttpEntity entity = response.getEntity();
System.out.println(entity.getContentType());
System.out.println(entity.getContentLength());
System.out.println(entity.getContentEncoding());
} catch (Exception e) {
e.printStackTrace();
}
输出
Content-Type: text/html; charset=utf-8
-1
null
HttpEntity提供了多种方法来读取正文,官方文档推荐使用以下方法进行读取:HttpEntity.getContent()
与 HttpEntity.writeTo(outputStream)
HttpEntity.getContent()方式
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
BufferedReader reader = null;
try {
httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
response = httpclient.execute(get);
HttpEntity entity = response.getEntity();
reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String str = "";
StringBuilder sb = new StringBuilder();
while ((str = reader.readLine()) != null) {
sb.append(str).append("\n");
}
System.out.println(sb);
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.closeReader(reader);
CommonUtils.closeResponse(response);
CommonUtils.closeHttpClient(httpclient);
}
CommonUtils
public void closeReader(BufferedReader reader) {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeResponse(CloseableHttpResponse response) {
try {
if (response != null) {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeHttpClient(CloseableHttpClient httpclient) {
try {
if (httpclient != null) {
httpclient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
HttpEntity.writeTo方式
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try {
httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
response = httpclient.execute(get);
HttpEntity entity = response.getEntity();
entity.writeTo(outstream);
System.out.println(outstream.toString("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.closeOutputStream(outstream);
CommonUtils.closeResponse(response);
CommonUtils.closeHttpClient(httpclient);
}
CommonUtils
public void closeOutputStream(ByteArrayOutputStream outstream) {
try {
if (outstream != null) {
outstream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
为了确保资源的正确释放,采用上述两种方式读取正文时,都需对流进行关闭。除此之外,HttpClient还提供了EntityUtils工具类,方便对正文的读取操作,不过官方文档中建议,只有当响应实体来自受信任的HTTP服务器,并且已知其长度有限,才可以采用这种方法。
EntityUtils.toString方式
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.closeHttpClient(httpclient);
}
EntityUtils.toString内部会对HttpEntity中的输入流进行关闭。
以上三种读取方式,当CloseableHttpClient的实例不再使用时,都需调用close方法进行关闭。
通过BufferedHttpEntity实现响应多次读取
对于流类型的HttpEntity而言,是不可重复读取的,若想多次读取,则需要在某个地方将HttpEntity缓存起来,最简单的方式,可以使用HttpClient提供的BufferedHttpEntity类来实现。
HttpEntity.getContent()+BufferedHttpEntity实现多次读取
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
System.out.println(getEntityContent(bufferedEntity));
System.out.println(getEntityContent(bufferedEntity));
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.closeHttpClient(httpclient);
}
public String getEntityContent(HttpEntity entity) {
try(BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"))) {
String str = "";
StringBuilder sb = new StringBuilder();
while ((str = reader.readLine()) != null) {
sb.append(str).append("\n");
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
HttpEntity.writeTo+BufferedHttpEntity实现多次读取
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
System.out.println(getContentByWriteTo(bufferedEntity));
System.out.println(getContentByWriteTo(bufferedEntity));
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.closeHttpClient(httpclient);
}
public String getContentByWriteTo(HttpEntity entity) {
try(ByteArrayOutputStream outstream = new ByteArrayOutputStream()) {
entity.writeTo(outstream);
return outstream.toString("UTF-8");
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
EntityUtils.toString+BufferedHttpEntity实现多次读取
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet get = new HttpGet("http://www.jianshu.com/u/8a3115bb299c");
try(CloseableHttpResponse response = httpclient.execute(get)) {
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
System.out.println(EntityUtils.toString(bufferedEntity, "UTF-8"));
System.out.println(EntityUtils.toString(bufferedEntity, "UTF-8"));
} catch (Exception e) {
e.printStackTrace();
} finally {
CommonUtils.closeHttpClient(httpclient);
}
至此,通过HttpClient,简单实现了http get请求的发送和响应处理。