public class HttpClientUtil {
/**
* HTTP协议
*/
private static final String HTTPS_PROTOCOL = "https://";
/**
* 协议默认端口
*/
private static final int HTTPS_PROTOCOL_DEFAULT_PORT = 443;
/**
* 默认编码格式
*/
private static final String DEFAULT_CHARSET = "UTF-8";
/**
* GET请求带参带token
* @param url 请求路径
* @param param map参数
* @param authorization token值
* @return
*/
public static String doGet(String url, Map<String, String> param,String authorization) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
if(!EmptyUtil.isEmpty(authorization)){
httpGet.setHeader(ApiConst.TOKEN_NAME, ApiConst.TOKEN_VAL +authorization);
}
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
/**
* GET请求不带参
* @param url 请求路径
* @return
*/
public static String doGet(String url) {
return doGet(url, null,null);
}
/**
* GET请求带参
* @param url 请求路径
* @param param 请求参数
* @return
*/
public static String doGet(String url, Map<String, String> param){
return doGet(url,param,null);
}
/**
* POST请求 带参带token
* @param url 请求地址
* @param param 请求参数
* @param authorization token
* @return
*/
public static String doPost(String url, Map<String, String> param,String authorization) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if(!EmptyUtil.isEmpty(authorization)){
httpPost.setHeader(ApiConst.TOKEN_NAME, ApiConst.TOKEN_VAL +authorization);
}
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
String va1 = param.get(key);
paramList.add(new BasicNameValuePair(key, va1));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"UTF-8");
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(),"utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
resultString=String.valueOf(JSON.parse(resultString));
return resultString;
}
/**
* POST请求 不带参数
* @param url 请求地址
* @return
*/
public static String doPost(String url) {
return doPost(url, null,null);
}
/**
* POST请求 带参数
* @param url 请求地址
* @param param 请求参数
* @return
*/
public static String doPost(String url,Map<String, String> param) {
return doPost(url, param,null);
}
/**
* POST请求 参数JSON格式 带参,带token
* @param url 请求地址
* @param json 请求参数
* @param authorization 请求token
* @return
*/
public static String doPostJson(String url, String json,String authorization) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
if(!EmptyUtil.isEmpty(authorization)){
httpPost.setHeader(ApiConst.TOKEN_NAME, ApiConst.TOKEN_VAL +authorization);
}
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
/**
* POST请求 参数JSON格式 带参
* @param url 请求地址
* @param json 请求参数
* @return
*/
public static String doPostJson(String url, String json){
return doPostJson(url,json,null);
}
/**
* DELETE请求 参数JSON格式
* @param url 请求地址
* @param json 参数
* @param authorization 令牌
* @return
*/
public static String doDelete(String url,String json,String authorization){
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpDeleteWithBody httpDeleteWithBody = new HttpDeleteWithBody(url);
if(!EmptyUtil.isEmpty(authorization)){
httpDeleteWithBody.setHeader(ApiConst.TOKEN_NAME, ApiConst.TOKEN_VAL +authorization);
}
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpDeleteWithBody.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpDeleteWithBody);
resultString = EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
/**
* PUT请求 参数JSON格式
* @param url
* @param json
* @param authorization
* @return
*/
public static String doPut(String url,String json,String authorization){
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPut httpPut = new HttpPut(url);
if(!EmptyUtil.isEmpty(authorization)){
httpPut.setHeader(ApiConst.TOKEN_NAME, ApiConst.TOKEN_VAL + authorization);
}
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPut.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPut);
resultString = EntityUtils.toString(response.getEntity(), DEFAULT_CHARSET);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
private static List<NameValuePair> SortParam(Map param) throws UnsupportedEncodingException {
List<NameValuePair> paramList = new ArrayList<>();
Map<String, String> tmpMap = param;
List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(tmpMap.entrySet());
// 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
return (o1.getKey()).toString().compareTo(o2.getKey());
}
});
// 构造URL 键值对的格式
StringBuilder buf = new StringBuilder();
for (Map.Entry<String, String> item : infoIds) {
if (StringUtils.isNotBlank(item.getKey())) {
String key = item.getKey();
String val = item.getValue();
val = URLEncoder.encode(val, "utf-8");
paramList.add(new BasicNameValuePair(key, val));
}
}
return paramList;
}
(JAVA)Http请求大全
最新推荐文章于 2024-05-25 21:07:00 发布