import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public static String get(String url, String cookie) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
// 设置 Cookie
connection.setRequestProperty("Cookie", cookie);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
response.append("\n");
}
reader.close();
return response.toString();
} else {
throw new IOException("Request failed with HTTP status: " + responseCode);
}
}
public Integer createAgencyToAppealSubmit(String subject, String contant, String adviceNo, List<String> userCodes) {
List<String> orderNoList = new ArrayList<>();
orderNoList.add(adviceNo);
List<AmsAbnormalOrderRecord> records = amsAbnormalOrderRecordMapper.selectByOrderNoList(orderNoList);
logJson = "createAgencyToAppealSubmit发送参数:";
SingleLangTodoDto dto = new SingleLangTodoDto();
dto.setDocSubject("您有一个流程需要处理:" + subject);
dto.setFdDocSubject(subject);
dto.setFdLongContent(contant);
dto.setFdLink(appealSubmitH5url+adviceNo);
Sequence sequence = new Sequence(3);
Long aLong = sequence.nextId();
dto.setFdKey(CommonConstants.CDR_KEY);
dto.setFdModelName(CommonConstants.CDR_KEY);
dto.setFdDocTime(System.currentTimeMillis());
dto.setFdFlowId(aLong.toString());
dto.setFdWorkitemId(aLong.toString());
dto.setFdAuthName(mideaAuthService.getUserCode());
dto.setFdSendersAccount(userCodes);
dto.setFdIsMobile(true);
dto.setType("todo");
dto.setFdType(3);
logger.info("发送代办参数:{}",JSON.toJSONString(dto));
byte[] bData = JSON.toJSONString(dto).getBytes();
//89df9b28-ebfe-4e15-b91c-245d98cdb2aa 该值联系mip配置生成
String token = DataEncrypt.encryptByte(bData, mipToken);
HttpURLConnection conn = null;
for(AmsAbnormalOrderRecord item:records){
item.setFdFlowId(String.valueOf(aLong));
}
try {
logger.info("发送代办请求:{}",mipUrl + URLEncoder.encode(token, "utf-8"));
logJson+=mipUrl + URLEncoder.encode(token, "utf-8");
// 创建url资源
URL url = new URL(mipUrl
+ URLEncoder.encode(token, "utf-8"));
// 建立http连接
conn = (HttpURLConnection) url.openConnection();
// 设置允许输出
conn.setDoOutput(true);
conn.setDoInput(true);
// 设置不用缓存
conn.setUseCaches(false);
// 设置传递方式
conn.setRequestMethod("POST");
// 设置维持长连接
conn.setRequestProperty("Connection", "Keep-Alive");
// 设置文件字符集:
conn.setRequestProperty("Charset", "UTF-8");
// 转换为字节数组
// 设置文件长度
conn.setRequestProperty("Content-Length", String.valueOf(bData.length));
// 设置文件类型:
conn.setRequestProperty("Content-Type", "application/json");
// 开始连接请求
conn.connect();
try (OutputStream out = conn.getOutputStream()) {
// 写入请求的字符串
out.write(bData);
out.flush();
}
// 请求返回的状态
if (conn.getResponseCode() == 200) {
logger.info("连接成功");
// 请求返回的数据
InputStream in = conn.getInputStream();
String result = null;
try {
byte[] data1 = new byte[in.available()];
in.read(data1);
// 转成字符串
result = new String(data1);
logger.info(result);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
logger.info("返回状态:" + conn.getResponseCode());
InputStream in = conn.getInputStream();
String result = null;
try {
byte[] data1 = new byte[in.available()];
in.read(data1);
// 转成字符串
result = new String(data1);
logger.info("请求代办返回结果:{}",result);
logJson+=" 返回结果:"+result;
} catch (Exception e1) {
e1.printStackTrace();
}
}
} catch (Exception e) {
logger.info("申诉-mipHelper error:{}",e);
} finally {
Integer am = amsAbnormalOrderRecordMapper.updateBatch(records);
logger.info("申诉-mipHelper amsAbnormalOrderRecordMapper,am:{}",am);
List<AmsAbnormalOrderLog> orderLogs = new ArrayList<>();
records.forEach(item->{
AmsAbnormalOrderLog orderLog =new AmsAbnormalOrderLog();
BeanUtils.copyProperties(item,orderLog);
orderLog.setCreateTime(new Date());
orderLog.setUpdateTime(new Date());
orderLog.setDeleteFlag(ActionEnum.DeleteFlagEnum.NO.getValue());
orderLog.setRemark(logJson);
orderLogs.add(orderLog);
});
Integer a = amsAbnormalOrderLogService.addAbnormalOrderLogList(orderLogs);
if(a==0){
logger.error("mip日志记录--mipHelper createAgencyToAppealSubmit, a:{}", a);
}
if (null != conn){
conn.disconnect();
}
}
return 1;
}
标签:info,调用,dto,String,http,接口,new,logger,conn From: https://www.cnblogs.com/huangwentian/p/17919313.html