首页 > 其他分享 >http调用接口

http调用接口

时间:2023-12-21 16:13:22浏览次数:28  
标签:info 调用 dto String http 接口 new logger conn

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

相关文章

  • ICEE-Interface-SATA的数据与电源接口
    **SATA数据接口(7pins)SATA电源接口(15pins4Sections:+12V,+5V,+3.3V,GND)**Sata实物:......
  • 河南南阳医专部署接口
    ///w##class(PISService.API.DHCPISReport).GetReportInfoByAdmNoToEMR("145111")///Desc:2023-02-10yangshun标板提供的获取病历报告的方法///Debug:w##class(DHCEPRFS.BL.HISInfo.BLPISInfo).GetPISPDFPath("145111")ClassMethodGetPISPDFPath(AEpisodeI......
  • 鸿蒙原生应用/元服务开发-Stage模型能力接口(九)上
    @ohos.app.ability.UIAbility(UIAbility)一、说明UIAbility是包含UI界面的应用组件,提供组件创建、销毁、前后台切换等生命周期回调,同时也具备组件协同的能力,组件协同主要提供如下常用功能。Caller,由startAbilityByCall接口返回,CallerAbility(调用者)可使用Caller与CalleeAbility(......
  • 09 信息打点-CDN 绕过篇&漏洞回链&接口探针&全网扫描&反向邮件
    一、知识点1.1CDN知识-工作原理及阻碍1.1.1CND概念CDN的全称是ContentDeliveryNetwork,即内容分发网络。其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输得更快、更稳定。通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一......
  • 【业务安全实战演练】业务接口调用模块测试9
    业务接口调用模块1,接口调用重放测试测试方法:接口调用重放测试可以理解成重放测试,接口也就是数据请求,功能很多,例如发布文章,发布评论,下订单,也可以理解成只要请求有新的数据生成,能重复请求并成功,都可以算请求重放,也就是接口重放测试。修复方法:对生成订单缓解可以使用验证码,防止生......
  • 解决先异步调用一个接口,再根据返回值继续循环异步,然后再同步的场景.then的连续调用
    场景描述:人员select下拉框为分页只查20个数据下拉(真实数据可能下拉有大几千),但是编辑页进来的id不在这20个数据下拉之内,所以显示的就是id值数组(因为是多选下拉框)解决:编辑回显方法内写getUserSecList(data.collaboratorVoList);然后先调人员20个下拉,调完后,根据传参的编辑i......
  • API接口防刷的9种方案
    防火墙:配置防火墙规则,限制对API接口的访问频率和来源IP,防止大量无效请求。验证码:在需要保护的接口中添加验证码验证,要求用户在访问前先进行验证码验证,以确认其为真实用户。IP限制:限制对API接口的访问仅限于特定IP范围,例如只允许内网或特定合作伙伴的IP访问。接口访问频......
  • HTTP1.0、HTTP1.1、HTTP2.0的区别
    当谈到网络通信协议时,大家都熟悉的就是HTTP(HypertextTransferProtocol),它是一种用于在Web浏览器和服务器之间传输数据的协议。随着技术的发展,HTTP也在不断演进,从HTTP1.0发展到了HTTP2.0,带来了许多新的特性和改进。 一、如何通过浏览器查看当前的http版本:谷歌: 火狐:......
  • libevent http-server示例
    wgethttps://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gztarxvzflibevent-2.1.12-stable.tar.gz-C.cdlibevent-2.1.12-stablemkdircmake-build-debugcdcmake-build-debugcmake../-DCMAKE_BUILD_TYPE......
  • 使用Postman测试需要授权的接口问题
    Postman是一款免费的http模拟请求工具,常用来测试开发接口。实际场景中,很多接口是需要授权才能使用。这就需要Postman获取授权,把返回的token保存,在调用其他接口时连同token一起传递。我所在的项目基于spring全家桶,正常流程是:调用获取验证码接口->登录接口->调用业务接口。新建......