WebService学习总结(七)——通过URLConnection调用webService接口,解析wsdl

这篇文章主要利用URLconnection调用webService接口,并利用dom4解析返回的数据

1.wsdl数据(访问http://localhost:8081/ERPDEMO/service/users?wsdl)

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.demo.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.demo.com/" name="UserServiceImplService" targetNamespace="http://impl.service.demo.com/">
<wsdl:import location="http://localhost:8081/ERPDEMO/service/users?wsdl=UserService.wsdl" namespace="http://service.demo.com/"></wsdl:import>
<wsdl:binding name="UserServiceImplServiceSoapBinding" type="ns1:UserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getPermissions">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getPermissions">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getPermissionsResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="selectByUserName">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="selectByUserName">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="selectByUserNameResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="login">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="login">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="loginResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="insert">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="insert">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="insertResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getRoles">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getRoles">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getRolesResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getUserById">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getUserById">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getUserByIdResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserServiceImplService">
<wsdl:port binding="tns:UserServiceImplServiceSoapBinding" name="UserServiceImplPort">
<soap:address location="http://localhost:8081/ERPDEMO/service/users"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

2.利用soapui获得你所有方法的soap格式数据

3.开始写方法,我将调用方法和dom4j解析方法写在一起,后期再分开吧

    public static void main(String[] args) throws Exception {
        // WebService服务的地址
        URL url = new URL("http://localhost:8081/ERPDEMO/service/users?wsdl");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        // 是否具有输入参数
        conn.setDoInput(true);
        // 是否输出输入参数
        conn.setDoOutput(true);
        // 发POST请求
        conn.setRequestMethod("POST");
        // 设置请求头(注意一定是xml格式)
        conn.setRequestProperty("content-type", "text/xml;charset=utf-8");
        String username = "csdn1";
        // 构造请求体,符合SOAP规范(最重要的)
        String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.demo.com/\" "
                + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                + " <soapenv:Header/>"
                + " <soapenv:Body>"
 
                + "<ser:selectByUserName>"
                + "<userName>"
                + username
                + "</userName>"
                + "</ser:selectByUserName>"
 
                + " </soapenv:Header/>"
                + "</soapenv:Body>"
                + "</soapenv:Envelope>";
 
        // 获得一个输出流
        OutputStream out = conn.getOutputStream();
        out.write(requestBody.getBytes());
 
        // 获得服务端响应状态码
        int code = conn.getResponseCode();
        StringBuffer sb = new StringBuffer();
        if (code == 200) {
            // 获得一个输入流,读取服务端响应的数据
            InputStream is = conn.getInputStream();
            byte[] b = new byte[1024];
            int len = 0;
 
            while ((len = is.read(b)) != -1) {
                String s = new String(b, 0, len, "utf-8");
                sb.append(s);
            }
            is.close();
        }
 
        out.close();
        System.out.println("服务端响应数据为:" + sb.toString());
 
        // 初始化报文,调用parse方法,获得结果map,然后按照需求取得字段,或者转化为其他格式
        Map map = new test01().parse(sb.toString());
        // 获得字段的值;
        String email = map.get("email").toString();
        String id = map.get("id").toString();
        String password = map.get("password").toString();
        String roleid = map.get("roleid").toString();
        String username1 = map.get("username").toString();
        System.out.println("email==" + email);
        System.out.println("id==" + id);
        System.out.println("password==" + password);
        System.out.println("roleid==" + roleid);
        System.out.println("username1==" + username1);
 
    }
dom4j解析方法

public Map<String, Object> map = new HashMap<String, Object>();
 
    /**
     * 解析soap
     * 
     * @param soap
     * @return
     * @throws DocumentException
     */
    public Map parse(String soap) throws DocumentException {
        Document doc = DocumentHelper.parseText(soap);// 报文转成doc对象
        Element root = doc.getRootElement();// 获取根元素,准备递归解析这个XML树
        getCode(root);
        return map;
    }
 
    /**
     * 遍历
     * 
     * @param root
     */
    public void getCode(Element root) {
        if (root.elements() != null) {
            List<Element> list = root.elements();// 如果当前跟节点有子节点,找到子节点
            for (Element e : list) {// 遍历每个节点
                if (e.elements().size() > 0) {
                    getCode(e);// 当前节点不为空的话,递归遍历子节点;
                }
                if (e.elements().size() == 0) {
                    map.put(e.getName(), e.getTextTrim());
                }// 如果为叶子节点,那么直接把名字和值放入map
            }
        }
 
    }
下面是返回的数据

1.soap返回的数据格式

服务端响应数据为:<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:selectByUserNameResponse xmlns:ns1="http://service.demo.com/"><return>
<email>111qq.com</email>
<id>1</id>
<password>123</password><roleid>1</roleid><username>csdn1</username>
</return>
</ns1:selectByUserNameResponse></soap:Body>
</soap:Envelope>
2.dom4j解析后的结果

email==111qq.com
id==1
password==123
roleid==1
username1==csdn1

--------------------- 
作者:changhenshui1990 
来源:CSDN 
原文:https://blog.csdn.net/changhenshui1990/article/details/79796418 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/smxjant/article/details/89399178