http请求优化

This commit is contained in:
15856 2024-09-04 09:20:41 +08:00
parent 088fdd2ed1
commit 245306c2e1
2 changed files with 10 additions and 19 deletions

View File

@ -17,6 +17,7 @@ import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@ -26,11 +27,11 @@ public class HttpHelper {
System.out.println("JSONBody-=========:" + JSONBody);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(JSONBody));
httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
httpPost.setEntity(new StringEntity(JSONBody,StandardCharsets.UTF_8));
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
String responseContent = EntityUtils.toString(entity, StandardCharsets.UTF_8);
response.close();
httpClient.close();
return responseContent;

View File

@ -187,22 +187,21 @@ public class MaMachineServiceImpl implements MaMachineService {
if (maMachineList.isEmpty()){
throw new ServiceException(String.format(ExceptionDict.PARAM_IS_NULL_ERROR_MSG,"maMachineList"), ExceptionDict.PARAM_IS_NULL_ERROR);
}
//遍历推送防止数据量过大
String content = JSONObject.toJSONString(maMachineList);
String encrypt;
try {
encrypt = RsaUtil.encryptByPublicKey(content, Constants.publicKey);
//encrypt = RsaUtil.encryptByPublicKey(content, Constants.publicKey);
Map<String, String> map = new HashMap<String, String>();
map.put("body", encrypt);
map.put("body", content);
String body = JSONObject.toJSONString(map);
String data = HttpHelper.sendHttpPost(zlptUrl, body);
log.info("dataString-=========:" + data);
//对返回的结果进行处理
resultDataHandler(data);
// resultDataHandler(data);
} catch (Exception e) {
// throw new RuntimeException(e);
return AjaxResult.success("请求成功");
return AjaxResult.error("推送失败");
}
return AjaxResult.success("请求成功!");
}
@ -211,22 +210,13 @@ public class MaMachineServiceImpl implements MaMachineService {
JSONObject object = JSONObject.parseObject(data);
System.err.println(data);
String code = object.getString("code");
if ("200".equals(code)) {
if ("0".equals(code)) {
String dataResultString = object.getString("data");
// 数据解密
String dataArrayString = RsaUtil.decryptByPrivateKey(dataResultString, Constants.publicKey);
log.info("dataArrayString-=========:" + dataArrayString);
JSONArray dataArray = JSONArray.parseArray(dataArrayString);
if (dataArray != null && dataArray.size() > 0) {
}else {
}
} else {
throw new Exception("推送失败!");
}
}
}