问题描述:我们经常会遇到请求外部系统接口的业务场景,restTemplate可能会存在请求不通或者获取不到返回报文的情况
解决方案:该工具类中有一个sendPostdataNew方法,使用post 的方式进行数据的请求,会将外部系统的接口返回报文已JSON字符串的形式返回,然后通过调用下面方法就可以将JSON字符串数据解析到我们提前封装好的和外部系统返参Vo一样的实体类中
//resultDate为调用外部接口返回的JSON字符串,ResultVo则是手动封装的接收参数的VO
ResultVo Vo = JsonUtil.transToRequest(resultData, ResultVo.class);
下面是这个工具类的详细代码,小伙伴们可以取走直用:
@Slf4j
public class HttpUtils {
private static final int CONNECT_TIMEOUT = 10000;
/**
* 使用post 的方式进行数据的请求
*
* @param strUrlPath 请求数据路径
* @param data 请求体参数
* @return 返回String
*/
public static String sendPostData(String strUrlPath, String data) {
return sendPostData(strUrlPath, data, "utf-8");
}
/**
* 使用post 的方式进行数据的请求
*
* @param strUrlPath 请求数据路径
* @param data 请求体参数
* @return 返回String
*/
public static String sendPostData(String strUrlPath, String data, String charsetName) {
HttpURLConnection httpURLConnection = null;
OutputStream outputStream = null;
InputStream inptStream = null;
//获得请求体
try {
URL url = new URL(strUrlPath);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setConnectTimeout(50000);
httpURLConnection.setReadTimeout(50000);
// 设置请求体的类型是文本类型
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// 设置请求体的长度
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.getBytes(charsetName).length));
// 设置编码格式
httpURLConnection.setRequestProperty("Accept-Charset", charsetName);
// 获得输出流,向服务器写入数据
outputStream = httpURLConnection.getOutputStream();
outputStream.write(data.getBytes(charsetName));
int response = httpURLConnection.getResponseCode();
// 请求成功
if (response == HttpsURLConnection.HTTP_OK) {
inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream, charsetName);
}
} catch (Exception e) {
e.printStackTrace();
log.error("http请求异常",e);
return "";
} finally {
if (inptStream != null) {
try {
inptStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return "";
}
/**
* 使用post 的方式进行数据的请求
*
* @param strUrlPath 请求数据路径
* @param data 请求体参数
* @return 返回String
*/
public static String sendPostDataNew(String strUrlPath, String data) {
//获得请求体
try {
URL url = new URL(strUrlPath);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(CONNECT_TIMEOUT);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setUseCaches(false);
httpURLConnection.setConnectTimeout(50000);
httpURLConnection.setReadTimeout(50000);
// 设置请求体的类型是文本类型
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// 设置请求体的长度
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.getBytes("utf-8").length));
// 设置编码格式
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
// 获得输出流,向服务器写入数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data.getBytes("utf-8"));
int response = httpURLConnection.getResponseCode();
// 请求成功
if (response == HttpsURLConnection.HTTP_OK) {
InputStream inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
return "-1";
}
return "-1";
}
/**
* 处理参数
*/
public static StringBuffer getRequestData(Map<String, String> params, String encode) {
StringBuffer stringBuffer = new StringBuffer();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), encode)).append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}
/**
* 处理返回结果
*/
public static String dealResponseResult(InputStream inputStream, String charsetName) throws Exception {
String resultData = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while ((len = inputStream.read(data)) != -1) {
byteArrayOutputStream.write(data, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
resultData = new String(byteArrayOutputStream.toByteArray(), charsetName);
return resultData;
}
public static void main(String[] args) {
StringBuffer allMsg = new StringBuffer();
allMsg.append("{");
allMsg.append("\"policyNo\": \"827122020110100000094\"," +
"\"InsuredName\": \"sunny1\"," +
"\"IdentifyNumber\": \"t1526164\"," +
"\"sign\": \"1234567890123456\"");
allMsg.append("}");
System.out.println(allMsg.toString());
HttpUtils.sendPostData("http://10.69.10.61:8080/mms-middle/Electronic/ElectronicdownPdf", allMsg.toString());
}
}