1.maven项目pom.xml中添加依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
2.创建HttpClientService Java类
package com.modules.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
/**
-
HttpClient发送GET、POST请求
-
@author yuqiang
-
@date 2020-04-28
/
public class HttpClientService {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClient.class);
/*
* 返回成功状态码
*/
private static final int SUCCESS_CODE = 200;/**
* 发送GET请求-
@param url 请求url
-
@param nameValuePairList 请求参数
-
@return JSON或者字符串
-
@throws Exception
*/
public static Object sendGet(String url, List nameValuePairList) throws Exception{
JSONObject jsonObject = null;
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try{
//创建HttpClient对象
client = HttpClients.createDefault();
//创建URIBuilder
URIBuilder uriBuilder = new URIBuilder(url);
//设置参数
uriBuilder.addParameters(nameValuePairList);
//创建HttpGet
HttpGet httpGet = new HttpGet(uriBuilder.build());
//设置请求头部编码
httpGet.setHeader(new BasicHeader(“Content-Type”, “application/x-www-form-urlencoded; charset=utf-8”));
//设置返回编码
httpGet.setHeader(new BasicHeader(“Accept”, “text/plain;charset=utf-8”));
//请求服务
response = client.execute(httpGet);
//获取响应码
int statusCode = response.getStatusLine().getStatusCode();if (SUCCESS_CODE == statusCode){ //获取返回对象 HttpEntity entity = response.getEntity(); //通过EntityUitls获取返回内容 String result = EntityUtils.toString(entity,"UTF-8"); //转换成json,根据合法性返回json或者字符串 try{ jsonObject = JSONObject.parseObject(result); return jsonObject; }catch (Exception e){ return result; } }else{ LOGGER.error("HttpClient-line: {}, errorMsg{}", 97, "GET请求失败!"); }
}catch (Exception e){
LOGGER.error(“HttpClient-line: {}, Exception: {}”, 100, e);
} finally {
response.close();
client.close();
}
return null;
}
/**
* 发送POST请求-
@param url
-
@param nameValuePairList
-
@return JSON或者字符串
-
@throws Exception
*/
public static Object sendPost(String url,JSONObject json){
// List nameValuePairList = null;JSONObject jsonObject = null;
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try{
//创建一个httpclient对象
client = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost(url);
//包装成一个Entity对象
// StringEntity entity = new UrlEncodedFormEntity(nameValuePairList, “UTF-8”);
StringEntity entity = new StringEntity(json.toJSONString(), “UTF-8”);
entity.setContentType(new BasicHeader(“Content-Type”, “application/json; charset=utf-8”));
//设置请求的内容
post.setEntity(entity);
//设置请求的报文头部的编码
// post.setHeader(new BasicHeader(“Content-Type”, “application/x-www-form-urlencoded; charset=utf-8”));
post.setHeader(new BasicHeader(“Content-Type”, “application/json; charset=utf-8”));
//设置请求的报文头部的编码
post.setHeader(new BasicHeader(“Accept”, “text/plain;charset=utf-8”));
//执行post请求
response = client.execute(post);
//获取响应码
int statusCode = response.getStatusLine().getStatusCode();
if (SUCCESS_CODE == statusCode){
//通过EntityUitls获取返回内容
String result = EntityUtils.toString(response.getEntity(),“UTF-8”);
//转换成json,根据合法性返回json或者字符串
try{
jsonObject = JSONObject.parseObject(result);
return jsonObject;
}catch (Exception e){
return result;
}
}else{
LOGGER.error(“HttpClient-line: {}, errorMsg:{}”, 146, “POST请求失败!”);
}
}catch (Exception e){
LOGGER.error(“HttpClient-line: {}, Exception:{}”, 149, e);
}finally {
try {
response.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
- 组织请求参数{参数名和参数值下标保持一致}
- @param params 参数名数组
- @param values 参数值数组
- @return 参数对象
/
public static List getParams(Object[] params, Object[] values){
/*- 校验参数合法性
*/
boolean flag = params.length>0 && values.length>0 && params.length == values.length;
if (flag){
List nameValuePairList = new ArrayList<>();
for(int i =0; i<params.length; i++){
nameValuePairList.add(new BasicNameValuePair(params[i].toString(),values[i].toString()));
}
return nameValuePairList;
}else{
LOGGER.error(“HttpClientService-line: {}, errorMsg:{}”, 197, “参数解析失败”);
}
return null;
}
- 校验参数合法性
/**
- 组织请求参数{参数名和参数值下标保持一致}
- @param params 参数名数组
- @param values 参数值数组
- @return 参数对象(JSON对象)
/
public static JSONObject getJsonParams(Object[] params, Object[] values){
/*- 校验参数合法性
*/
boolean flag = params.length>0 && values.length>0 && params.length == values.length;
if (flag){
JSONObject jsonObject = new JSONObject();
for(int i =0; i<params.length; i++){
jsonObject.put(params[i].toString(),values[i]);
}
return jsonObject;
}else{
LOGGER.error(“HttpClientService-line: {}, errorMsg:{}”, 197, “参数解析失败”);
}
return null;
}
}
- 校验参数合法性
-
3.调用方式
//调用get
//参数名
Object [] params = new Object[]{param1,param2...};
//参数值
Object [] values = new Object[]{val1,val2...};
//获取参数对象
List<NameValuePair> paramsList = HttpClientService.getParams(params, values);
//发送get
Object result = null;
try {
result = HttpClientService.sendGet(url, paramsList);
} catch (Exception e) {
e.printStackTrace();
}
//调用post
//参数名
Object [] params = new Object[]{param1,param2...};
//参数值(json对象结构的参数)
JSONObject dataJsonObject = new JSONObject();
JSONObject name1 = new JSONObject();
name1.put("value", "测试医生");
JSONObject time2 = new JSONObject();
time2.put("value", "2020年05月06日 10:21");
JSONObject thing3 = new JSONObject();
thing3.put("value", "处方已开具,请查看");
dataJsonObject.put("name1", name1);
dataJsonObject.put("time2", time2);
dataJsonObject.put("thing3", thing3);
Object [] values = new Object[]{val1,val2...,dataJsonObject};
//获取参数对象
JSONObject json = HttpClientService.getJsonParams(params, values);
//发送post
Object result = HttpClientService.sendPost(url, json);