java发送soap请求和解析soap的响应

一.soap请求报文

<s:Envelope
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <GetPoints xmlns="http://tempuri.org/">
            <kunkun xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            	<!-- 下面是需要传的四个参数 -->
                <d4p1:string>chang</d4p1:string>  
                <d4p1:string>tiao</d4p1:string>  
                <d4p1:string>rap</d4p1:string>  
                <d4p1:string>basketball</d4p1:string>                
            </kunkun>
        </GetPoints>
    </s:Body>
</s:Envelope>

二.soap响应报文

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <GetPointsResponse xmlns="http://tempuri.org/">
            <GetPointsResult xmlns:a="http://schemas.datacontract.org/2004/07/XDB.DataServiceInterface" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                <a:Data>
                    <a:Name>张三</a:Name>
                    <a:LikeYear>2.5年</a:LikeYear>        
                </a:Data>
                <a:Data>
                    <a:Name>李四</a:Name>
                    <a:LikeYear>2.5年</a:LikeYear>        
                </a:Data>
                <a:Data>
                    <a:Name>王二</a:Name>
                    <a:LikeYear>2.5年</a:LikeYear>        
                </a:Data>
                <a:Data>
                    <a:Name>麻子</a:Name>
                    <a:LikeYear>2.5年</a:LikeYear>        
                </a:Data>
            </GetPointsResult>
        </GetPointsResponse>
    </s:Body>
</s:Envelope>

三.代码实现

1.引入对应jar

   <dependency>
	   <groupId>dom4j</groupId>
	   <artifactId>dom4j</artifactId>
    </dependency>

2.代码实现

public boolean testSoap() {
        StringBuilder result = new StringBuilder();
        OutputStream out = null;
        BufferedReader in = null;
        //需要传的参数
        String[] pointNames = {"chang","tiao","rap","basketball"};
        //拼接请求报文的方法
        String soap = buildXML(pointNames);
        try {
            URL url = new URL(你需要请求的url地址);
            URLConnection connection = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) connection;
            byte[] b = soap.getBytes("ISO-8859-1");
            httpConn.setRequestProperty( "Content-Length",String.valueOf( b.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( b );
            in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));

            //把响应回来的报文拼接为字符串
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                result.append(inputLine);
            out.close();
            in.close();
            //把soap的xml报文转为list
            Document doc = DocumentHelper.parseText(result.toString());//报文转成doc对象
            Element root = doc.getRootElement();//获取根元素,准备递归解析这个XML树
            Map<String, String> map = new HashMap<String, String>();
            List<Map<String, String>> lists = new ArrayList<Map<String, String>>();//存放叶子节点数据
            //获取叶子节点的方法
            getCode(root, map,lists);
            //循环叶子节点数据
            for (Map<String, String> item : lists) {
                //取响应报文中的name
                String key = item.get("Name");
                //取响应报文中的LikeYear
                String value =  item.get("LikeYear");
                //你需要的后续操作
            }
        } 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 true;
    }


   /**
     * 拼接报文
     * @param pointNames
     * @return
     */
    private static String buildXML(String[] pointNames){

        String str = "<s:Envelope\n" +
                "    xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <s:Body>\n" +
                "        <GetPoints\n" +
                "            xmlns=\"http://tempuri.org/\">\n" +
                "            <kunkun\n" +
                "                xmlns:d4p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"\n" +
                "                xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
        for (String s : pointNames) {
            str += "<d4p1:string>" + s + "</d4p1:string>"+"\n";
        }
        str += "</kunkun>\n" +
                "        </GetPoints>\n" +
                "    </s:Body>\n" +
                "</s:Envelope>";

        return str;
    }

	 /**
     * 找到soap的xml报文的叶子节点的数据
     * @param root
     * @param map
     * @param lists
     */
    public static void getCode(Element root, Map<String, String> map,List<Map<String, String>> lists) {
        if (root.elements() != null) {
            List<Element> list = root.elements();//如果当前跟节点有子节点,找到子节点
            for (Element e : list) {//遍历每个节点
                if (e.elements().size() > 0) {
                    getCode(e, map ,lists);//当前节点不为空的话,递归遍历子节点;
                }
                if (e.elements().size() == 0) {
                    //如果为叶子节点,那么直接把名字和值放入map
                    map.put(e.getName(), e.getTextTrim());
                    //如果数据都放进map,那就存进list
                    if (map.size() == 2){ //注意:因为这里我响应回来的叶子节点是两个,所以等于2,如果你的不是请修改。对应的就是<Name>和<LikeYear>这是两个叶子节点
                        Map<String, String> mapTo = new HashMap<String, String>();
                        //map全部赋值给maoTo
                        mapTo.putAll(map);
                        lists.add(mapTo);
                        //清空map
                        map.clear();
                    }
                }
            }
        }
    }
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Java发送SOAP报文,可以使用SOAP协议和相关的Java库来实现。 首先,你需要使用Java的Web服务相关的库来创建一个SOAP客户端。最常用的库是JAX-WS(Java API for XML Web Services)和Apache Axis。这些库提供了方便的API,用于创建和发送SOAP报文。 在使用这些库之前,你需要先定义一个WSDL(Web Services Description Language)文件,其中描述了你的SOAP服务的接口和操作。你可以使用WSDL编辑器或者手动编写WSDL文件。然后,利用JAX-WS提供的工具来生成Java类,这些类将用于创建和解析SOAP报文。 接下来,你需要创建一个SOAP客户端。使用生成的Java类,你可以通过调用相应的方法来构建SOAP请求报文。然后,将构建的SOAP请求报文发送SOAP服务的URL。这可以通过在Java程序中使用HTTP连接来实现。你可以使用Java提供的URLConnection或Apache HttpClient等库来发送HTTP请求,并将SOAP请求作为HTTP请求的正文发送。 最后,你需要处理SOAP服务返回的SOAP响应报文。通过解析SOAP响应报文,你可以提取其中的数据并进行相应的处理。 总结起来,实现Java发送SOAP报文的步骤包括: 1. 定义WSDL文件描述SOAP服务接口和操作。 2. 使用JAX-WS生成Java类。 3. 创建SOAP客户端,构建SOAP请求报文,并发送SOAP服务的URL。 4. 处理SOAP服务的响应报文,提取数据并进行相应的处理。 以上是一个简单的概述,实际的实现可能会涉及更多的细节和配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱穿背带裤的馫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值