package com.vci.rmip.code.client.codeapply.Apply410.utils;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
|
import com.vci.rmip.code.client.codeapply.Apply410.object.R;
|
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.http.*;
|
import org.apache.http.client.ClientProtocolException;
|
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.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
|
import java.io.IOException;
|
import java.net.URISyntaxException;
|
import java.util.ArrayList;
|
import java.util.LinkedList;
|
import java.util.List;
|
import java.util.Map;
|
|
public class HttpUtil {
|
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
/**
|
* 发送HttpGet请求
|
* @param url
|
* @return
|
*/
|
public static String sendGet(String url) {
|
|
HttpGet httpget = new HttpGet(url);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httpget);
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
String result = null;
|
try {
|
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
result = EntityUtils.toString(entity);
|
}
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
response.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 发送HttpGet带参请求
|
* @param url
|
* @param params
|
* @return
|
*/
|
public static R sendGet(String url, Map<String, String> params, Map<String,String> headers) {
|
R r=new R();
|
// 获取连接客户端工具
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
String entityStr = null;
|
CloseableHttpResponse response = null;
|
try {
|
/*
|
* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数
|
*/
|
URIBuilder uriBuilder = new URIBuilder(url);
|
/** 第一种添加参数的形式 */
|
/*uriBuilder.addParameter("name", "root");
|
uriBuilder.addParameter("password", "123456");*/
|
/** 第二种添加参数的形式 */
|
List<NameValuePair> list = new LinkedList<>();
|
//设置头部
|
for(Map.Entry<String,String> entry:params.entrySet()){
|
BasicNameValuePair param1 = new BasicNameValuePair(entry.getKey(), entry.getValue());
|
list.add(param1);
|
}
|
uriBuilder.setParameters(list);
|
// 根据带参数的URI对象构建GET请求对象
|
HttpGet httpGet = new HttpGet(uriBuilder.build());
|
/*
|
* 添加请求头信息
|
*/
|
if(headers!=null&&headers.size()>0) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httpGet.addHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
// 执行请求
|
response = httpClient.execute(httpGet);
|
// 获得响应的实体对象
|
HttpEntity entity = response.getEntity();
|
// 使用Apache提供的工具类进行转换成字符串
|
entityStr = EntityUtils.toString(entity, "UTF-8");
|
if(StringUtils.isNotBlank(entityStr)){
|
r = JSONObject.toJavaObject(JSONObject.parseObject(entityStr), R.class);
|
}
|
} catch (ClientProtocolException e) {
|
System.err.println("Http协议出现问题");
|
e.printStackTrace();
|
} catch (ParseException e) {
|
System.err.println("解析错误");
|
e.printStackTrace();
|
} catch (URISyntaxException e) {
|
System.err.println("URI解析异常");
|
e.printStackTrace();
|
} catch (IOException e) {
|
System.err.println("IO异常");
|
e.printStackTrace();
|
} finally {
|
// 释放连接
|
if (null != response) {
|
try {
|
response.close();
|
httpClient.close();
|
} catch (IOException e) {
|
System.err.println("释放连接出错");
|
e.printStackTrace();
|
}
|
}
|
}
|
return r;
|
}
|
/**
|
* 发送HttpGet带参请求
|
* @param url
|
* @param header
|
* @return
|
*/
|
public static String sendGet(String url, Map<String, String> header) {
|
HttpGet httpGet = new HttpGet(url);
|
|
|
//设置头部
|
for(Map.Entry entry:header.entrySet()){
|
// System.out.println(entry.getKey()+ "###########" + entry.getValue());
|
httpGet.setHeader(entry.getKey().toString(),entry.getValue().toString());
|
}
|
// System.out.println(jsonObject.toString());
|
|
|
// HttpGet httpget = new HttpGet(url);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httpGet);
|
} catch (IOException e1) {
|
e1.printStackTrace();
|
}
|
String result = null;
|
try {
|
HttpEntity entity = response.getEntity();
|
if (entity != null) {
|
result = EntityUtils.toString(entity);
|
}
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
response.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
/**
|
* 发送HttpPost请求,参数为map
|
* @param url
|
* @param jsonDataStr
|
* @return
|
*/
|
public static R sendPost(String url, String jsonDataStr,Map<String,String> headers) {
|
R r=new R();
|
// JsonObject formparams = new JsonObject();
|
// for (Map.Entry<String, String> entry : map.entrySet()) {
|
// formparams.add(entry.getKey(), entry.getValue();
|
// }
|
//json 格式
|
//UrlEncodedFormEntity entity = new UrlEncodedFormEntity(jsonDataStr, Consts.UTF_8);
|
// System.out.println(jsonObject.toString());
|
StringEntity entity = new StringEntity(jsonDataStr, Consts.UTF_8);
|
HttpPost httppost = new HttpPost(url);
|
/*
|
* 添加请求头信息
|
*/
|
if(headers!=null&&headers.size()>0) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httppost.addHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
httppost.setEntity(entity);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httppost);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
HttpEntity entity1 = response.getEntity();
|
String result = null;
|
try {
|
result = EntityUtils.toString(entity1, "UTF-8");
|
// 使用Apache提供的工具类进行转换成字符串
|
if(StringUtils.isNotBlank(result)){
|
r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class);
|
}
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
}
|
return r;
|
}
|
/**
|
* 发送HttpPost请求,参数为map
|
* @param url
|
* @param map
|
* @return
|
*/
|
public static R sendPost(String url, Map<String,String> map,Map<String,String> headers) {
|
R r=new R();
|
// JsonObject formparams = new JsonObject();
|
// for (Map.Entry<String, String> entry : map.entrySet()) {
|
// formparams.add(entry.getKey(), entry.getValue();
|
// }
|
//json 格式
|
// UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
|
|
JsonObject jsonObject = new JsonObject();
|
for(Map.Entry entry:map.entrySet()){
|
// System.out.println(entry.getKey()+ "###########" + entry.getValue());
|
jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
|
}
|
// System.out.println(jsonObject.toString());
|
StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
|
HttpPost httppost = new HttpPost(url);
|
/*
|
* 添加请求头信息
|
*/
|
if(headers!=null&&headers.size()>0) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httppost.addHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
httppost.setEntity(entity);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httppost);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
HttpEntity entity1 = response.getEntity();
|
String result = null;
|
try {
|
result = EntityUtils.toString(entity1, "UTF-8");
|
// 使用Apache提供的工具类进行转换成字符串
|
if(StringUtils.isNotBlank(result)){
|
r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class);
|
}
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
}
|
return r;
|
}
|
/**
|
* 发送HttpPost请求,参数为map
|
* @param url
|
* @param map
|
* @return
|
*/
|
public static String sendPost(String url, Map<String,String> map) {
|
// JsonObject formparams = new JsonObject();
|
// for (Map.Entry<String, String> entry : map.entrySet()) {
|
// formparams.add(entry.getKey(), entry.getValue();
|
// }
|
//json 格式
|
// UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
|
JsonObject jsonObject = new JsonObject();
|
for(Map.Entry entry:map.entrySet()){
|
// System.out.println(entry.getKey()+ "###########" + entry.getValue());
|
jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
|
}
|
// System.out.println(jsonObject.toString());
|
StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
|
HttpPost httppost = new HttpPost(url);
|
httppost.setEntity(entity);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httppost);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
HttpEntity entity1 = response.getEntity();
|
String result = null;
|
try {
|
result = EntityUtils.toString(entity1);
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
/**
|
* 发送HttpPost请求,参数为map
|
* @param url
|
* @param dataMap
|
* @return
|
*/
|
public static String sendFormPost(String url, Map<String,String> dataMap, Map<String,String> headers) {
|
String result = null;
|
try {
|
|
//json 格式
|
List<NameValuePair> nvps = new ArrayList<>();
|
//
|
HttpPost httppost = new HttpPost(url);
|
if (dataMap != null && dataMap.size() > 0) {
|
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
|
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
}
|
}
|
/*
|
* 添加请求头信息
|
*/
|
if (headers != null && headers.size() > 0) {
|
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
httppost.addHeader(entry.getKey(), entry.getValue());
|
}
|
}
|
httppost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); //将参数传入post方法中
|
//httppost.setEntity(entity);
|
CloseableHttpResponse response = null;
|
response = httpclient.execute(httppost);
|
HttpEntity entity1 = response.getEntity();
|
|
result = EntityUtils.toString(entity1, "UTF-8");
|
// 使用Apache提供的工具类进行转换成字符串
|
if (StringUtils.isNotBlank(result)) {
|
return result;
|
}
|
}catch (Throwable e){
|
e.printStackTrace();
|
}
|
return result;
|
}
|
/**
|
* 发送不带参数的HttpPost请求
|
* @param url
|
* @return
|
*/
|
public static String sendPost(String url) {
|
HttpPost httppost = new HttpPost(url);
|
CloseableHttpResponse response = null;
|
try {
|
response = httpclient.execute(httppost);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
HttpEntity entity = response.getEntity();
|
String result = null;
|
try {
|
result = EntityUtils.toString(entity);
|
} catch (ParseException | IOException e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
|
}
|