HttpMethod工具类
程序员文章站
2022-09-08 23:27:45
package com.dzbh.nonghang.utils;import com.alibaba.fastjson.JSON;import com.dzbh.nonghang.pojo.TestResult;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.H...
package com.dzbh.nonghang.utils;
import com.alibaba.fastjson.JSON;
import com.dzbh.nonghang.pojo.TestResult;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class HttpMethod {
public static final String guaranteeApplyURL="http://127.0.0.1:8080/nh/select";
// 测试申请保函
public static void main(String[] args) throws Exception {
Map<String,Object> map = new HashMap<>();
map.put("orderId","订单号由外部机构生成");
map.put("bankCode","金融机构编号");
map.put("sectionCode","标段(项目)名称");
map.put("projectCode","标段(项目)编号");
map.put("bidderName","投标人名称");
map.put("bidderCode","投标人统一社会信用代码");
map.put("bidderAddress","投标人地址");
map.put("bidderContactPhoneNumber","投标人电话");
map.put("tenderer","招标(采购)人名称");
map.put("tendererCode","招标人统一信用社会代码");
map.put("tendererContactPhone","招标人电话");
map.put("tendererAddress","招标(采购)人地址");
map.put("tenderBond",1.55);
map.put("tenderStartTime","投标生效日期(开标时间)yyyy-MM-dd");
map.put("tenderExpire","投标有效期至");
map.put("signStr","验签加密串");
String s = JSON.toJSONString(map);
//System.out.println("转为json后:"+ JSON.toJSONString(map));
//String url = "http://39.101.186.230:80/select";
String url = "http://127.0.0.1:8080/nh/select";
// 发起请求
String resutlt = connect(url, s);
System.out.println("响应结果----->"+resutlt);
// 序列化成对象
TestResult result = JSON.parseObject(resutlt, TestResult.class);
System.out.println("getErrorCode:-->"+result.getErrorCode());
System.out.println("getErrorMsg:-->"+result.getErrorMsg());
System.out.println("getJumpUrl:-->"+result.getJumpUrl());
/*System.out.println("orderApply:---->"+ orderApply.toString());
System.out.println("orderApply:"+ orderApply);
System.out.println("orderId:"+ orderApply.getOrderId());*/
}
public static String connect(String url, String info) throws IOException {
// 建立连接
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
conn.setConnectTimeout(8000);
conn.setRequestMethod("POST");
conn.setInstanceFollowRedirects(false);
// 设置json格式传递参数
conn.setRequestProperty("Content-Type",
"application/json;charset=utf-8");// 设置参数类型是json格式
conn.setDoOutput(true);
OutputStreamWriter out = null;
StringBuffer str = new StringBuffer();
BufferedReader reader = null;
try {
// 发送数据
out = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
out.write(info);
out.flush();
// 获取数据
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
// 接收数据
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
reader.close();
out.close();
} catch (Exception e) {
// TODO: handle exception
}finally{
reader.close();
out.close();
}
return str.toString();
}
}
本文地址:https://blog.csdn.net/weixin_45724648/article/details/109641962