
目录
 
- 1.前言
 - 2.请求报文格式
 - 2.1不带表头的请求格式
 - 2.2带表头的请求格式
 
- 3 请求代码实例
 - 3.1解析Soap返回的XML,提取需要的元素
 
- 参考
 
文章所属专区 超链接
1.前言
SOAP请求(Simple Object Access Protocol,简单对象访问协议)是HTTP POST的一个专用版本,遵循一种特殊的XML消息格式,Content-type设置为:text/xml ,任何数据都可以XML化。
SOAP:简单对象访问协议。SOAP是一种轻量的,简单的,基于XML的协议,它被设计成在web上交换结构化的和固化的信息。SOAP可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。
2.请求报文格式
在使用SOAP请求时,我们需要明确请求的Method,即要请求的Web服务所提供的方法名,不同的Web服务API会提供不同的方法名,具体使用时需要根据API文档进行查
 
2.1不带表头的请求格式
 <?xml version="1.0" encoding="UTF-8"?>
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com">
   <SOAP-ENV:Body>
      <ns1:getWeather>
         <ns1:city>Beijing</ns1:city>
      </ns1:getWeather>
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
 
2.2带表头的请求格式
 <?xml version="1.0" encoding="UTF-8"?>
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com">
   <SOAP-ENV:Header>
      <ns1:Auth>
         <ns1:Username>testUser</ns1:Username>
         <ns1:Password>testPassword</ns1:Password>
      </ns1:Auth>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>
      <ns1:getUserData>
         <ns1:userId>12345</ns1:userId>
      </ns1:getUserData>
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
 
3 请求代码实例
   public String getUrlBySoap(String token,String appKey,String xmlStrSM4) {
        StringBuilder result = new StringBuilder();
        String resultUrl = "";
        OutputStream out = null;
        BufferedReader in = null;
        //需要传的参数
        //String[] pointNames = {"chang","tiao","rap","basketball"};
        //拼接请求报文的方法
        String soap =  buildXML(token,appKey,xmlStrSM4).toString();
        try {
            URL url = new URL(evaluation_url);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) connection;
            byte[] soapBytes = soap.getBytes("ISO-8859-1");
            httpConn.setRequestProperty( "Content-Length",String.valueOf( soapBytes.length ) );
            httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
            httpConn.setRequestProperty("soapaction","http://tempuri.org/WcfDataProxy/GetPoints");//重点中的重点,不加就500。注意:soapaction对应的值不固定,具体值看你的请求。
            httpConn.setRequestMethod( "POST" );
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            
            out = httpConn.getOutputStream();
            out.write( soapBytes );
            int responseCode = httpConn.getResponseCode();
            if(responseCode == 200){
                in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "utf-8"));
                //把响应回来的报文拼接为字符串
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    result.append(inputLine);
                }
                out.close();
                in.close();
                //报文转成doc对象
                Document doc = DocumentHelper.parseText(result.toString());
                //获取根元素,准备递归解析这个XML树
                Element root = doc.getRootElement();
                Map<String, String> map = new HashMap<String, String>();
                //存放叶子节点数据
                List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
                //获取叶子节点的方法
                String leafNode = "";
                leafNode = getCode(root);
                  
 
                if(leafNode != null && leafNode != ""){
                    resultUrl = getUrlByDom4j(leafNode);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  catch (ParseException e) {
            e.printStackTrace();
        } catch (org.dom4j.DocumentException e) {
            e.printStackTrace();
        } finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return resultUrl;
    }
 
3.1解析Soap返回的XML,提取需要的元素
    /**
     * 找到soap的xml报文的叶子节点的数据
     * @param root
     */
    public String getCode(Element root) throws DocumentException {
        String result = "";
        if (root.elements() != null) {
            //如果当前跟节点有子节点,找到子节点
            List<Element> list = root.elements();
            //遍历每个节点
            for (Element e : list) {
                if (e.elements().size() > 0) {
                    //当前节点不为空的话,递归遍历子节点;
                    result=getCode(e);
                    if(result != null && result != ""){
                        return result;
                    }
                }
                if (e.elements().size() == 0) {
                    result = e.getTextTrim();
                    return result;
                }
            }
        }else{
            return root.getTextTrim();
        } 
        return result;
    }
 
参考
使用 Postman 发送 SOAP 请求的步骤与方法
给个三连吧 谢谢谢谢谢谢了
 


















![2023年中国TFT-LCD面板产业链、需求量及市场规模分析[图]](https://img-blog.csdnimg.cn/img_convert/c5a0ae2372310a91ba423fdffc3909b5.png)
