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

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

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


  
  
  1. <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/">
  2. <wsdl: import location= "http://localhost:8081/ERPDEMO/service/users?wsdl=UserService.wsdl" namespace= "http://service.demo.com/"></wsdl: import>
  3. <wsdl:binding name= "UserServiceImplServiceSoapBinding" type= "ns1:UserService">
  4. <soap:binding style= "document" transport= "http://schemas.xmlsoap.org/soap/http"/>
  5. <wsdl:operation name= "getPermissions">
  6. <soap:operation soapAction= "" style= "document"/>
  7. <wsdl:input name= "getPermissions">
  8. <soap:body use= "literal"/>
  9. </wsdl:input>
  10. <wsdl:output name= "getPermissionsResponse">
  11. <soap:body use= "literal"/>
  12. </wsdl:output>
  13. </wsdl:operation>
  14. <wsdl:operation name= "selectByUserName">
  15. <soap:operation soapAction= "" style= "document"/>
  16. <wsdl:input name= "selectByUserName">
  17. <soap:body use= "literal"/>
  18. </wsdl:input>
  19. <wsdl:output name= "selectByUserNameResponse">
  20. <soap:body use= "literal"/>
  21. </wsdl:output>
  22. </wsdl:operation>
  23. <wsdl:operation name= "login">
  24. <soap:operation soapAction= "" style= "document"/>
  25. <wsdl:input name= "login">
  26. <soap:body use= "literal"/>
  27. </wsdl:input>
  28. <wsdl:output name= "loginResponse">
  29. <soap:body use= "literal"/>
  30. </wsdl:output>
  31. </wsdl:operation>
  32. <wsdl:operation name= "insert">
  33. <soap:operation soapAction= "" style= "document"/>
  34. <wsdl:input name= "insert">
  35. <soap:body use= "literal"/>
  36. </wsdl:input>
  37. <wsdl:output name= "insertResponse">
  38. <soap:body use= "literal"/>
  39. </wsdl:output>
  40. </wsdl:operation>
  41. <wsdl:operation name= "getRoles">
  42. <soap:operation soapAction= "" style= "document"/>
  43. <wsdl:input name= "getRoles">
  44. <soap:body use= "literal"/>
  45. </wsdl:input>
  46. <wsdl:output name= "getRolesResponse">
  47. <soap:body use= "literal"/>
  48. </wsdl:output>
  49. </wsdl:operation>
  50. <wsdl:operation name= "getUserById">
  51. <soap:operation soapAction= "" style= "document"/>
  52. <wsdl:input name= "getUserById">
  53. <soap:body use= "literal"/>
  54. </wsdl:input>
  55. <wsdl:output name= "getUserByIdResponse">
  56. <soap:body use= "literal"/>
  57. </wsdl:output>
  58. </wsdl:operation>
  59. </wsdl:binding>
  60. <wsdl:service name= "UserServiceImplService">
  61. <wsdl:port binding= "tns:UserServiceImplServiceSoapBinding" name= "UserServiceImplPort">
  62. <soap:address location= "http://localhost:8081/ERPDEMO/service/users"/>
  63. </wsdl:port>
  64. </wsdl:service>
  65. </wsdl:definitions>

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



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


  
  
  1. public static void main(String[] args) throws Exception {
  2. // WebService服务的地址
  3. URL url = new URL( "http://localhost:8081/ERPDEMO/service/users?wsdl");
  4. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  5. // 是否具有输入参数
  6. conn.setDoInput( true);
  7. // 是否输出输入参数
  8. conn.setDoOutput( true);
  9. // 发POST请求
  10. conn.setRequestMethod( "POST");
  11. // 设置请求头(注意一定是xml格式)
  12. conn.setRequestProperty( "content-type", "text/xml;charset=utf-8");
  13. String username = "csdn1";
  14. // 构造请求体,符合SOAP规范(最重要的)
  15. String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.demo.com/\" "
  16. + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
  17. + " <soapenv:Header/>"
  18. + " <soapenv:Body>"
  19. + "<ser:selectByUserName>"
  20. + "<userName>"
  21. + username
  22. + "</userName>"
  23. + "</ser:selectByUserName>"
  24. + " </soapenv:Header/>"
  25. + "</soapenv:Body>"
  26. + "</soapenv:Envelope>";
  27. // 获得一个输出流
  28. OutputStream out = conn.getOutputStream();
  29. out.write(requestBody.getBytes());
  30. // 获得服务端响应状态码
  31. int code = conn.getResponseCode();
  32. StringBuffer sb = new StringBuffer();
  33. if (code == 200) {
  34. // 获得一个输入流,读取服务端响应的数据
  35. InputStream is = conn.getInputStream();
  36. byte[] b = new byte[ 1024];
  37. int len = 0;
  38. while ((len = is.read(b)) != - 1) {
  39. String s = new String(b, 0, len, "utf-8");
  40. sb.append(s);
  41. }
  42. is.close();
  43. }
  44. out.close();
  45. System.out.println( "服务端响应数据为:" + sb.toString());
  46. // 初始化报文,调用parse方法,获得结果map,然后按照需求取得字段,或者转化为其他格式
  47. Map map = new test01().parse(sb.toString());
  48. // 获得字段的值;
  49. String email = map.get( "email").toString();
  50. String id = map.get( "id").toString();
  51. String password = map.get( "password").toString();
  52. String roleid = map.get( "roleid").toString();
  53. String username1 = map.get( "username").toString();
  54. System.out.println( "email==" + email);
  55. System.out.println( "id==" + id);
  56. System.out.println( "password==" + password);
  57. System.out.println( "roleid==" + roleid);
  58. System.out.println( "username1==" + username1);
  59. }

dom4j解析方法


  
  
  1. public Map<String, Object> map = new HashMap<String, Object>();
  2. /**
  3. * 解析soap
  4. *
  5. * @param soap
  6. * @return
  7. * @throws DocumentException
  8. */
  9. public Map parse(String soap) throws DocumentException {
  10. Document doc = DocumentHelper.parseText(soap); // 报文转成doc对象
  11. Element root = doc.getRootElement(); // 获取根元素,准备递归解析这个XML树
  12. getCode(root);
  13. return map;
  14. }
  15. /**
  16. * 遍历
  17. *
  18. * @param root
  19. */
  20. public void getCode(Element root) {
  21. if (root.elements() != null) {
  22. List<Element> list = root.elements(); // 如果当前跟节点有子节点,找到子节点
  23. for (Element e : list) { // 遍历每个节点
  24. if (e.elements().size() > 0) {
  25. getCode(e); // 当前节点不为空的话,递归遍历子节点;
  26. }
  27. if (e.elements().size() == 0) {
  28. map.put(e.getName(), e.getTextTrim());
  29. } // 如果为叶子节点,那么直接把名字和值放入map
  30. }
  31. }
  32. }

下面是返回的数据

1.soap返回的数据格式


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


  
  
  1. email== 111qq.com
  2. id== 1
  3. password== 123
  4. roleid== 1
  5. username1==csdn1



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值