主要解决http请求支持get,post,put,delete等常规方法,支持@RequestParam,@RequestBody,@PathVariable等参数格式传输,支持传输附件同时传递参数等,主体代码如下:
package mes.client.action;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import mes.client.pojo.po.CommonEntityPo;
import mes.client.pojo.vo.ResponseInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class MesMomConnectAction {
private static Logger logger = LoggerFactory.getLogger(MesMomConnectAction.class);
/**
* 无请求体body,向指定URL发送方法的请求
*
* @param url 发送请求的URL
* @return URL 所代表远程资源的响应结果
*/
public static ResponseInfo getHttpParamReq(String url,String requestMethodType) {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
HttpURLConnection connection = null;
try {
URL getUrl = new URL(url);
// 打开和URL之间的连接
connection = (HttpURLConnection) getUrl.openConnection();
// 在connect之前,设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Charsert", "UTF-8");
//添加必须的头部信息
addHeader(connection);
// 配置本次连接的Content-type,form表单是"application/x-www-form-urlencoded",json是"application/json"等
// 根据需求自己调整Content-type
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 设置连接方式:get
connection.setRequestMethod(requestMethodType);
// 建立实际的连接,可不写,注意connection.getOutputStream会隐含的进行connect。
connection.connect();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
ResponseInfo responseInfo = JSON.parseObject(result.toString(), ResponseInfo.class);
if(responseInfo.getStatusCode() == null){
return getFailResponse(result.toString());
}
return responseInfo;
} catch (Exception e) {
logger.error("调用MES4.0接口异常",e);
return getExceptionResponse(e);
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
if (connection != null) {
//关闭连接
connection.disconnect();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* @description: get请求
* @date: 2023-10-16 16:58:30
* @param url
* @return ResponseInfo
*/
public static ResponseInfo sendGet(String url){
return getHttpParamReq(url,"GET");
}
/**
* @description:方法说明
* @date: 2023-10-16 15:42:33
* @param url
* @return ResponseInfo
*/
public static ResponseInfo sendDelete(String url) {
return getHttpParamReq(url,"DELETE");
}
/**
* @description: post请求
* @date: 2023-10-16 17:04:07
* @param url
* @param param
* @return ResponseInfo
*/
public static ResponseInfo sendPost(String url, CommonEntityPo param){
return getHttpBodyReq(url,param,"POST");
}
/**
* @description: put请求
* @date: 2023-10-16 17:05:05
* @param url
* @param param
* @return ResponseInfo
*/
public static ResponseInfo sendPut(String url, CommonEntityPo param){
return getHttpBodyReq(url,param,"PUT");
}
/**
* 有请求体body,向指定 URL 发送方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数。
* @return 所代表远程资源的响应结果
*/
public static ResponseInfo getHttpBodyReq(String url, CommonEntityPo param, String requestMethodType) {
BufferedReader in = null;
InputStream inputStream = null;
HttpURLConnection connection = null;
StringBuilder result = new StringBuilder();
try {
URL postUrl = new URL(url);
// 打开和URL之间的连接
connection = (HttpURLConnection) postUrl.openConnection();
// 在connect之前,设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Charsert", "UTF-8");
//添加一些必要的请求头信息
addHeader(connection);
//connection.setRequestProperty("Authorization","Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJiUlR5WGJ0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5LP54WBXq0JyxnMSLTjqZWMfvYmxNLfjO3rcviK9jAqqxrvLy1IR4ZVFz8ez4ThfCr3TrbammQvZgxF1jie_hx_-3RTiLTra1qyTUj8CrFRaVDBvWTt8nDu6FsRBAkqImPgZ3Jdhf-WDeVlwOkDCGPd5cUQ9KAQs2JUMY4H_jC9cgcSpQuUjNOQOnu1lnYjnsREswR7iBNSkaW6DlRry-HhD8FRs1QLOwKGcgFCBU7O9O-NCH8EyrZOriSpiPaElE2-i2Q");
connection.setConnectTimeout(15000);
connection.setReadTimeout(60000);
// 发送POST请求必须设置如下两行,参数要放在http正文内
connection.setDoOutput(true);
connection.setDoInput(true);
// 默认是 GET方式
connection.setRequestMethod(requestMethodType);
// Post 请求不使用缓存
connection.setUseCaches(false);
// 配置连接的Content-type,form表单是"application/x-www-form-urlencoded",json是"application/json"等
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
//参数处理
if(param != null){
//打印接口入参
logger.info("post请求接口入参:{}",JSON.toJSONString(param.getData()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(JSON.toJSONString(param.getData()));
writer.close();
}
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
ResponseInfo responseInfo = JSON.parseObject(result.toString(), ResponseInfo.class);
if(responseInfo.getStatusCode() == null){
return getFailResponse(result.toString());
}
return responseInfo;
} catch (Exception e) {
logger.error("调用MES4.0接口异常",e);
return getExceptionResponse(e);
} finally {
try {
if (in != null) {
in.close();
}
if(inputStream != null){
inputStream.close();
}
if (connection != null) {
//关闭连接
connection.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/*public static void main(String[] args) throws IOException {
//=========================================邮箱==================================================
List