• SOAP请求-示例


    客户用的soap接口,请求的报文如下

    <?xml version="1.0" encoding="utf-8" ?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.inf.creditapp.app/">
      <soapenv:Header/>
      <soapenv:Body>
        <ws:search>
          <!--Optional:-->
          <request>
            <!--Optional:-->
            <brNo>_brNo</brNo>
            <!--Optional:-->
            <content>_content</content>
            <!--Optional:-->
            <reqDate>_reqDate</reqDate>
            <!--Optional:-->
            <reqSerial>_reqSerial</reqSerial>
            <!--Optional:-->
            <reqTime>_reqTime</reqTime>
            <!--Optional:-->
            <token>_token</token>
            <!--Optional:-->
            <txCode>_txCode</txCode>
          </request>
        </ws:search>
      </soapenv:Body>
    </soapenv:Envelope>

    curl请求模拟soap请求

    curl -X POST -H "Content-Type: text/xml" -d '<?xml version="1.0" encoding="utf-8" ?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.inf.creditapp.app/"><soapenv:Header/>
      <soapenv:Body><ws:search><request><brNo>****</brNo><content>{"batchNo":"***15428807928***","brNo":"015","dataCnt":1,"singleReqList":[{"appDate":"20181122","brNo":"015","cifName":"测试","crpReason":"02","czAuth":"1","czId":"1","czPactNo":"015_02_20181122_506100",
      "grantType":"01","idNo":"3302***2745","idType":"0","phoneNo":"18516260125","prdtName":"test",brTel:"18523652563"}]}</content><reqDate>20181122</reqDate><reqSerial>20181122180001477085</reqSerial><reqTime>18:00:01</reqTime><token>248A***D86C</token><txCode>4102</txCode>
      </request></ws:search></soapenv:Body>' http://30.2.2.2/mfs/services/searchService?wsdl
    String myNamespace = "ws"
                String myNamespaceURI = "http://ws.inf.creditapp.app/"
    
                // Create SOAP Connection
                SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance()
                soapConnection = soapConnectionFactory.createConnection()
                // Create SOAP Message
                MessageFactory messageFactory = MessageFactory.newInstance()
                soapMessage = messageFactory.createMessage()
                SOAPPart soapPart = soapMessage.getSOAPPart()
    
                // SOAP Envelope
                SOAPEnvelope envelope = soapPart.getEnvelope()
                envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI)
    
                // SOAP Body
                soapBody = envelope.getBody()
                SOAPElement soapBodyElem = soapBody.addChildElement("search", myNamespace)
    
                SOAPElement requestElem = soapBodyElem.addChildElement("request")
    
                SOAPElement brNoEle = requestElem.addChildElement("brNo")
                brNoEle.addTextNode(brNo)
                SOAPElement contentEle = requestElem.addChildElement("content")
                //flag
                println "version:--1--"
                String reqJson = JSON.toJSONString(foreignTradeApplyReq)
                logger.info(String.format("foreignTradeApplyReq json:"+ reqJson))
                System.out.println("end json")
                contentEle.addTextNode(reqJson)
                SOAPElement reqDate = requestElem.addChildElement("reqDate")
                reqDate.addTextNode(dateFormated)
                SOAPElement reqSerialEle = requestElem.addChildElement("reqSerial")
    
                reqSerialEle.addTextNode(serialNumber)//
                SOAPElement reqTime = requestElem.addChildElement("reqTime")
                reqTime.addTextNode(timeFormated)
                SOAPElement token = requestElem.addChildElement("token")
                token.addTextNode(crd_token)
                SOAPElement txCode = requestElem.addChildElement("txCode")
                txCode.addTextNode("***")
                soapMessage.saveChanges()
                soapMessage.writeTo(System.out)
              // Send SOAP Message to SOAP Server
                soapResponse = soapConnection.call(soapMessage, crd_apply_url)
              
    
    
                soapResponse.writeTo(System.out)
    //拿到结果
                SOAPBody soapBodyRes = soapResponse.getSOAPBody()
                NodeList nodeList = soapBodyRes.getElementsByTagName("response")
    
                for(int i = 0; i < nodeList.getLength(); i++){
                    NodeList itemList = nodeList.item(i).getChildNodes()
                    for (int j = 0 ; j < itemList.getLength(); j++){
                        String itemname = itemList.item(j).getNodeName()
                        String textContent = itemList.item(j)?.getTextContent()?.trim()
                        logger.warn(String.format("itemname: %s textContent: %s", itemname, textContent))
                        if(itemname.equalsIgnoreCase("respCode")){
                            foreignResObj.setRespCode(textContent)
                        }else if(itemname.equalsIgnoreCase("respDesc")){
                            foreignResObj.setRespDesc(textContent)
                        }else if(itemname.equalsIgnoreCase("content") && textContent != null){
                            def content = new groovy.json.JsonSlurper().parseText(textContent)
                            if(content instanceof Map){
                                foreignResObj.setContent(content)
                            }
                        }
                    }
                }
                response = JSON.toJSONString(foreignResObj)

    soap请求发送代码如上所示。

    欢迎关注Java流水账公众号
  • 相关阅读:
    QinQ基础知识
    mysql执行sql脚本时--force的作用
    docker的thin pool 和 ulimit问题
    使用MAT来进行java内存问题的简单分析
    自己对DHCP的理解
    根据类名找jar包和maven坐标
    curl在windows下和linux中使用的一个区别
    JAVA实现LRU算法
    经典买票并发
    AQS学习
  • 原文地址:https://www.cnblogs.com/guofu-angela/p/10067236.html
Copyright © 2020-2023  润新知