java调用WebService接口

 案例:

接口:

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测试:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值