案例:
接口:
http://xxxxx:8080/GetSPService.asmx
调用方法:GetSPByStnCodeToJsonStr
参数1:begin 开始时间 格式 yyyymmdd hh:mi (日和小时之间有空格) 例如:20230718 06:00
参数2: end 结束时间 格式 yyyymmdd hh:mi (日和小时之间有空格) 例如:20230719 06:00
参数3:stationcode 电报略码 例如:TPT,SJT
public String getPlanDataFromService() {
//请求url
String serverURL = "http://xxxx:8080/GetSPService.asmx/GetSPByStnCodeToJsonStr";
String results = null;
try {
URL url = new URL(serverURL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
//set the request method to POST
connection.setRequestMethod("POST");
//allow input
connection.setDoInput(true);
//allow output
connection.setDoOutput(true);
//header参数connection.setRequestProperty("键","值");
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
//body参数
LocalDateTime now = LocalDateTime.now();
String start = getDate(now);
LocalDateTime localDateTime = now.plusDays(1);
String end = getDate(localDateTime);
JSONObject parm2 = new JSONObject();
parm2.put("begin",start+" 06:00");
parm2.put("end", end+" 06:00");
parm2.put("stationcode","TPT");
writer.write(parm2.toString());
writer.flush();
StringBuffer sbf = new StringBuffer();
String strRead = null;
// 返回结果-字节输入流转换成字符输入流,控制台输出字符
InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
connection.disconnect();
results = sbf.toString();
System.out.println("-----解析存储数据开始---");
JSONObject dataObj = JSONObject.parseObject(result);
//todo 实际逻辑
} catch (IOException e) {
throw new RuntimeException(e);
}
return results;
}
Postman测试: