1简介
最近在南京出差时,在对接一个平台时,根据事件类型订阅关注的事件,通过传送目标地址,平台会自动推送相关的把请求体封装成JSONObject,自己呢则是需要对这部分的事件进行简单的数据组织,然后以一种特定的形式把数据发送到上一层。由于在程序运转时使用Http协议,因此尝试使用HttpClient来实现该功能。超文本传输协议HTTP是当今互联网上最火的传输协议,因此熟练的掌握HTTP协议的类型和含义,参数传递,header的含义,在程序员的日常生活中占据着很重要的作用。
2问题介绍
HttpClient官网介绍了HttpClient的使用。
HttpClient Tutorial,基本的HttpClient相关知识均可以在此页面获取
2.1Maven jar包引入
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-win</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.6</version>
</dependency>
2.2 HttpClient Quick Start
下面这部分的代码是HttpClient官网提供的demo演示,详细阐述了Get请求和Post请求的使用过程,具体代码如下:
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
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;
/**
* @Owner:
* @Time: 2019/2/25-21:13
*/
public class A {
public void demo() throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
// The underlying HTTP connection is still held by the response object
// to allow the response content to be streamed directly from the network socket.
// In order to ensure correct deallocation of system resources
// the user MUST call CloseableHttpResponse#close() from a finally clause.
// Please note that if response content is not fully consumed the underlying
// connection cannot be safely re-used and will be shut down and discarded
// by the connection manager.
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity1);
} finally {
response1.close();
}
HttpPost httpPost = new HttpPost("http://targethost/login");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
response2.close();
}
}
}
3代码解析
3.1 代码
在项目中,自己东拼西凑最后写了如下可以运行的代码,经验证,代码可以执行预期的功能,如下所示:
package com.cechi.controller;
import com.alibaba.fastjson.JSONObject;
import com.cetc52.platform.config.ServerConfig;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 处理上报的事件
*
* @Owner:
* @Time: 2019/1/30-10:12
*/
@Controller
@RequestMapping(value = "/eventService/api")
public class EventHandle {
private static String HTTP = "http";
@Autowired
private ServerConfig serverConfig;
@PostMapping("/eventSubscriptionByEventTypes")
@ResponseBody
public String eventUpload(@RequestBody JSONObject eventInfo) {
System.out.println(eventInfo.toJSONString());
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = serverConfig.getUrl()+ "/eventService/api/v1/eventHandleGet?userId=1000";
get(url);
String url2 = serverConfig.getUrl()+ "/eventService/api/v1/eventHandleGetMap1?a=b&b=c";
Map<String, String> param = new HashMap<>();
param.put("a", "b");
param.put("b", "c");
get(url2);
String postUrlMap = serverConfig.getUrl()+"/eventService/api/v1/eventHandlePostMap";
postMap(postUrlMap,param);
String postUrlJson = serverConfig.getUrl()+"/eventService/api/v1/eventHandlePostJson";
postJson(postUrlJson, JSONObject.toJSONString(param));
return eventInfo.toJSONString();
}
@GetMapping("/v1/eventHandleGet")
@ResponseBody
public void eventInfoComposing(@RequestParam String userId) {
System.out.println("eventInfoComposing begins");
System.out.println("userId = "+ userId);
return ;
}
@GetMapping("/v1/eventHandleGetMap1")
@ResponseBody
public void getMap1(@RequestParam String a, @RequestParam String b) {
System.out.println("getMap1 begins");
System.out.println(a+"&"+ b);
return ;
}
@PostMapping("/v1/eventHandlePostMap")
@ResponseBody
public String postMapImpl(@RequestBody JSONObject param) {
System.out.println("postMapImpl begins");
System.out.println(param.toJSONString());
return param.toJSONString();
}
@PostMapping("/v1/eventHandlePostJson")
@ResponseBody
public String postJsonImpl(@RequestBody JSONObject param) {
System.out.println("postJsonImpl begins");
System.out.println(param.toJSONString());
return param.toJSONString();
}
private String entityToString(HttpEntity entity) throws IOException {
String result = null;
if(entity != null)
{
long lenth = entity.getContentLength();
if(lenth != -1 && lenth < 2048)
{
result = EntityUtils.toString(entity,"UTF-8");
}else {
InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
CharArrayBuffer buffer = new CharArrayBuffer(2048);
char[] tmp = new char[1024];
int l;
while((l = reader1.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
result = buffer.toString();
}
}
return result;
}
/**
* get请求,参数拼接在地址上
* @param url 请求地址加参数
* @return 响应
*/
public String get(String url)
{
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(get);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* get请求,参数放在map里
* @param url 请求地址
* @param map 参数map
* @return 响应
*/
public String getMap(String url,Map<String,String> map)
{
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry : map.entrySet())
{
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
builder.setParameters(pairs);
HttpGet get = new HttpGet(builder.build());
response = httpClient.execute(get);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 发送post请求,参数用map接收
* @param url 地址
* @param map 参数
* @return 返回值
*/
public String postMap(String url,Map<String,String> map) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry : map.entrySet())
{
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(map), "utf-8");
stringEntity.setContentEncoding("utf-8");
stringEntity.setContentType("application/json");
post.setEntity(stringEntity);
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* post请求,参数为json字符串
* @param url 请求地址
* @param jsonString json字符串
* @return 响应
*/
public String postJson(String url,String jsonString)
{
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
post.setHeader("Content-Type","application/json" );
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
3.2 使用IDEA自带的Test Restful Web Service调试
Tools - > Http Client - > Test Restful Web Service
@PostMapping("/eventSubscriptionByEventTypes")
对应的接口为平台上报事件的接口,与事件相关的信息会保存在eventInfo参数中,在该接口中,预期要对eventInfo进行重新的事件信息整理,并通过HttpClient发送到对应的4个接口,其中两个get请求,两个Post请求,一个get请求不带参数,一个带参数,而两个Post请求一个参数的形式为map,一个参数的形式为JsonString。
3.2 Console结果
{"a":"b","b":"c"}
eventInfoComposing begins
userId = 1000
getMap1 begins
b&c
postMapImpl begins
{"a":"b","b":"c"}
postJsonImpl begins
{"a":"b","b":"c"}
可以看到通过调用
postMap(postUrlMap,param);
的请求,函数执行确实走到了四个对应的接口。
3.3 postMap和postJson函数分析
3.3.1postMap
public String postMap(String url,Map<String,String> map) {
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for(Map.Entry<String,String> entry : map.entrySet())
{
pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
CloseableHttpResponse response = null;
try {
post.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(map), "utf-8");
stringEntity.setContentEncoding("utf-8");
stringEntity.setContentType("application/json");
post.setEntity(stringEntity);
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
重点关注post请求header的设置以及Map参数的设置的过程。
3.3.2 postJson
public String postJson(String url,String jsonString)
{
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
try {
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
post.setHeader("Content-Type","application/json" );
response = httpClient.execute(post);
if(response != null && response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
result = entityToString(entity);
}
return result;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
if(response != null)
{
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
关注的重点也是在header的设置以及参数的设置。
注意:http://host:port/anno/obj?id=1&name=xx
整体才对应于url,而get请求的参数并非写在url的后main而是写入了Entity中。
3.4 HttpClient使用流程总结
- 创建HttpClient对象。
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 如果需要发送请求参数,可调用HttpGetsetParams方法来添加请求参数;对于HttpPost对象而言,可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse对象。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
- 释放连接。无论执行方法是否成功,都必须释放连接
4 总结
HttpClient是一个非常重要的知识点积累,对于程序员来说,自己也购买了图解HTTP这本经典书,弄清楚HTTP超文本协议的具体使用过程和header含义、工作原理对于程序员来说非常重要,值得认真的研究。