Hutool SoapClient 学习

Hutool SoapClient 学习一 由来在接口对接当中 WebService 接口占有着很大份额 而我们为了使用这些接口 不得不引入类似 Axis 等库来实现接口请求 现在有了 Hutool 就可以在无任何依赖的情况下 实现简便的 WebService 请求 二 使用使用 SoapUI 解析 WSDL 地址 找到 WebService 方法和参数 我们得到的 XML 模板为

一、由来

在接口对接当中,WebService接口占有着很大份额,而我们为了使用这些接口,不得不引入类似Axis等库来实现接口请求。

现在有了Hutool,就可以在无任何依赖的情况下,实现简便的WebService请求。

二、使用

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/"> <soapenv:Header/> <soapenv:Body> <web:getCountryCityByIp>  
    <web:theIpAddress>? 
     web:theIpAddress>  
      web:getCountryCityByIp>  
       soapenv:Body>  
        soapenv:Envelope> 
// 新建客户端 SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx") // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间 .setMethod("web:getCountryCityByIp", "http://WebXml.com.cn/") // 设置参数,此处自动添加方法的前缀:web .setParam("theIpAddress", "218.21.240.106"); // 发送请求,参数true表示返回一个格式化后的XML内容 // 返回内容为XML字符串,可以配合XmlUtil解析这个响应 Console.log(client.send(true)); 

三、设置消息头

在业务中有遇到一个问题,就是使用CXF动态生成的SoapClient去访问WebService,在idea上运行没有问题,但是打包之后就各种问题了。于是立即换了Hutool的SoapClient,但是有个问题就是官方文档只给出了最简单的例子。比如如下的xml模板我们该怎么实现?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <soapenv:Header> <tem:Header>  
    <tem:UserName>? 
     tem:UserName>  
     <tem:Password>? 
      tem:Password>  
       tem:Header>  
        soapenv:Header> <soapenv:Body> <tem:HelloWorld/>  
         soapenv:Body>  
          soapenv:Envelope> 

我们按照日常习惯,看看SoapClient里面有哪些方法,于是就看到了header()方法,刚好有两个参数,于是直接拿来使用,然后并没有起效。

SoapClient client = SoapClient.create("http://localhost:65168/WebService1.asmx") .setMethod("tem:HelloWorld","http://tempuri.org/") .header("UserName","mes") .header("Password",""); String soapXmlStr=client.send(); 

查看源代码发现是被添加到了一个MessageHeader中去了。MessageHeader是消息请求头,嗯,所以不要搞混了,跟我们要添加的Header不是一回事。

 public void addRequestProperty(String key, String value) { 
    checkConnected(); if (key == null) throw new NullPointerException ("key is null"); if (requests == null) requests = new MessageHeader(); requests.add(key, value); } 

四、设置Header

我们需要访问一个第三方WebService,需要我们传输一个MySoapHeader。使用SoapClient的 client.addSOAPHeader(name)方法,其他方法单独使用会报错。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <soapenv:Header> <tem:MySoapHeader>  
    <tem:UserName>? 
     tem:UserName>  
     <tem:Password>? 
      tem:Password>  
       tem:MySoapHeader>  
        soapenv:Header> <soapenv:Body> <tem:HelloWorld/>  
         soapenv:Body>  
          soapenv:Envelope> 

我们需要设置QName,然后自己创建xml子节点。

SoapClient.create("http://localhost:65168/WebService1.asmx") .setMethod("tem:HelloWorld","http://tempuri.org/") QName name = new QName("http://tempuri.org/","MySoapHeader",""); SOAPHeaderElement mySoapHeader = client.addSOAPHeader(name); SOAPElement UserName=mySoapHeader.addChildElement("UserName"); UserName.setTextContent("mes"); SOAPElement PassWord=mySoapHeader.addChildElement("PassWord"); PassWord.setTextContent(""); String soapXmlStr=client.send(); 

五、通过HttpClient进行访问

来自好心大哥的代码

HttpClient httpClient = new HttpClient(); tring url = wsconfigVO.getUrl(); String soapRequestData = " 
    
     
     
      
      
        " 
       
      
     
    + exportxml + " "; Logger.error("[gzcg]bw:"+soapRequestData); PostMethod httppost = new PostMethod(url); /* 把Soap请求数据添加到PostMethod */ try { 
    httppost.setRequestHeader("Content-Type", "text/xml;charset=UTF-8"); httppost.setRequestHeader("SOAPAction", ""); byte[] b = soapRequestData.getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/soap+xml; charset=utf-8"); httppost.setRequestEntity(re); httpClient.executeMethod(httppost); InputStream rs = httppost.getResponseBodyAsStream(); // 获取返回流并转换为Document Document document = new SAXReader().read(rs, "UTF-8"); String result = document.asXML(); Logger.error("[gzsyzr] backxml:"+result); // 符号转换 result = result.replaceAll("<", "<").replaceAll(">", ">"); result = result.substring(result.lastIndexOf(" 
   )); result = result.substring(0, result.indexOf("") + 6); Logger.error("[gzsyzr] backxml1:"+result); return new Object[]{ 
    result.trim()}; } catch (Exception e) { 
     Logger.error("调用接口服务报错:" + e.getMessage(), e); throw new BusinessException("调用esburlssl接口服务报错:" + e.getMessage(), e); } finally { 
     httppost.releaseConnection(); } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211859.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午9:25
下一篇 2026年3月18日 下午9:26


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号