• Java 客户端调用 WebService 接口的一种方式


    1. SoapUI 测试 WebService 接口

      通过SoapUI创建一个SOAP Project;
      项目名称自定义,WSDL地址维护WebService接口地址。点击OK即可

    在这里插入图片描述

      项目创建完成后,展开WebService项,可以看到具体的接口,打开接口下的Request,右侧面板Form标签下可以清晰的看到请求入参,点击Submit请求按钮可以看到Overview标签下的响应结果。
    在这里插入图片描述
      XML标签下的请求报文和响应报文

    
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.qyythpt.jzkj.com">
       <soapenv:Header/>
       <soapenv:Body>
          <web:sendMessage>
             
             
          <xmlStr>123456xmlStr>web:sendMessage>
       soapenv:Body>
    soapenv:Envelope>
    
    
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <soap:Body>
          <ns2:sendMessageResponse xmlns:ns2="http://webservice.qyythpt.jzkj.com">
             <String>1695087972825
    0
    数据处理错误!]]>String>
          ns2:sendMessageResponse>
       soap:Body>
    soap:Envelope>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2. Java 访问 WebService 接口

      WebService 调用有多种方式,此处以 HttpURLConnection 调用为例。
      接口请求封装,仅需要传入接口需要的入参和接口地址。
      接口响应结果,解析成Map对象返回。如下所示

    /**
         * WebService推送报文封装请求推送
         * WebService 接口地址为 http://{ip}:{port}/services/TraderService?wsdl
         * @param webServicePushUrl 此处 webServicePushUrl 为 http://{ip}:{port}/services/TraderService 
         * @param xmlStr 接口入参
         * @return
         */
        private Map<String, Object> webServicePush(String webServicePushUrl, String xmlStr) {
            Map<String, Object> resultMap = new HashMap<>(2);
            resultMap.put("code", "0");
            resultMap.put("msg", "推送失败!");
    
            InputStream is = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            OutputStream os = null;
            try {
                //第一步:创建服务地址,不是WSDL地址
                URL url = new URL(webServicePushUrl);
                //2:打开到服务地址的一个连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                //3:设置连接参数
                //3.1设置发送方式:POST必须大写
                connection.setRequestMethod("POST");
                //3.2设置数据格式:Content-type
                connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
                //3.3设置输入输出,新创建的connection默认是没有读写权限的,
                connection.setDoInput(true);
                connection.setDoOutput(true);
                //4:组织SOAP协议数据,发送给服务端;报文参考SoapUI测试工具的XML请求报文
                String soapXML = "" +
                        "" +
                        "" +
                        "" +
                        " +
                        xmlStr +
                        "]]>" +
                        "" +
                        "";
                logger.warn("[WebService]:推送请求{}", soapXML);
                os = connection.getOutputStream();
                os.write(soapXML.getBytes());
                //5:接收服务端的响应
                int responseCode = connection.getResponseCode();
                if(200 == responseCode){//表示服务端响应成功
                    is = connection.getInputStream();
                    isr = new InputStreamReader(is);
                    br = new BufferedReader(isr);
    
                    StringBuilder sb = new StringBuilder();
                    String temp = null;
                    while(null != (temp = br.readLine())){
                        sb.append(temp);
                    }
                    logger.warn("[WebService]:推送结果{}", sb);
                    Map<String, Map> bodyMap = XmlUtils.xmlTOMap(sb.toString());
                    Map<String, Map> sendMessageResponseMap = bodyMap.get("Body");
                    Map<String, String> resultStringMap = sendMessageResponseMap.get("sendMessageResponse");
                    String stringXML = resultStringMap.get("String");
                    stringXML = "" + stringXML + "";
                    Map<String, String> stringMap = XmlUtils.xmlTOMap(stringXML);
                    // String -> 16945051934421ok
                    // String -> 16788629279560数据处理错误!
                    // 0失败,1成功
                    resultMap.put("code", stringMap.get("rescode"));
                    resultMap.put("msg", stringMap.get("resmsg"));
                }
            } catch (Exception e) {
                logger.error("[WebService]推送异常", e);
            } finally {
                if (null != is) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                if (null != isr) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                if (null != br) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                if (null != os) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            return resultMap;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101

    更多请求方式可参考文档调用webservice服务方式总结
    Powered By niaonao

  • 相关阅读:
    Unity实现摄像头录像功能
    RDS:在 Windows Server 2016/2019 中的 RDS/RemoteApp 性能问题
    Linux 学习的六个过程
    浏览器下载快捷方式到桌面(PWA)
    【编程题】【Scratch四级】2021.12 森林运动会
    Rasa:使用大语言模型进行意图分类
    关于solidity解析abi方法,入参和结果字节码
    vue使用甘特图插件dhtmlx-gantt( 简单版)
    为什么要让img浮动:
    线程运行状态
  • 原文地址:https://blog.csdn.net/niaonao/article/details/133738424