关于用JAX-WS调用webservice异常:Server did not recognize the value of HTTP Header SOAPAction: .

来源:互联网 发布:天猫魔盒网络无法连接 编辑:程序博客网 时间:2024/06/05 08:49

在调用一个webservice接口的时候发现用wsimport 生成客户端代码调用正常,但是通过soap消息直接调用则抛异常:

Server did not recognize the value of HTTP Header SOAPAction: .


经过长时间搜索找到这篇文章:http://www.tuicool.com/articles/RBVrUv


文章指出“在实现SOAP规范1.1中JAX-WS并不需要SOAPAction,但.NET中是需要的,哪怕是空。这就导致了上面的异常。


在我这里调用的接口正是.net开发提供的。解决办法是:

1、JAX-WS中加入SOAPAction并设置其值为空,可惜JAX-WS没提供相关设置API,但CFX、axis等开源框架提供了API;2、设置.NET不使用SOAPAction,而使用<Body>XML 元素之后的第一个子元素做为路由RoutingStyle=SoapServiceRoutingStyle.RequestElement

作为接口调用方,我们可能没办法要求接口提供方作出修改,那么就可以采用第一种方法。经过实测,使用axis2调用成功。靠谱。axis依赖包如下:

<dependency>          <groupId>org.apache.axis2</groupId>          <artifactId>axis2</artifactId>          <version>1.5.1</version>          <type>pom</type>          <scope>compile</scope>      </dependency>      <dependency>          <groupId>org.apache.axis2</groupId>          <artifactId>axis2-adb</artifactId>          <version>1.5.1</version>          <type>jar</type>          <scope>compile</scope>      </dependency>      <dependency>          <groupId>org.apache.axis2</groupId>          <artifactId>axis2-kernel</artifactId>          <version>1.5.1</version>          <type>jar</type>          <scope>compile</scope>      </dependency>          <dependency>         <groupId>org.apache.axis2</groupId>         <artifactId>org.apache.axis2.osgi</artifactId>         <version>1.5.1</version>          </dependency>  

axis2的使用方法可参考这篇文章:http://blog.csdn.net/xiang520jl/article/details/15504175


当然了,用客户端代码调用也没什么不好。


由于怕第一篇参考文章链接失效,在这里,拷贝原文:


Java调用.Net写的WebService报异常:服务器未能识别 HTTP 头 SOAPAction 的值

异常及解决方案

使用JAX-WS调用.Net写的WebService报了如下异常:

javax.xml.ws.soap.SOAPFaultException: System.Web.Services.Protocols.SoapException: 服务器未能识别 HTTP 头 SOAPAction 的值: ....
网络上很多信息都提示说,给.NET的WebService类(即.asmx文件下的类)添加属性:
[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)]
即可解决。


但你知道为什么吗?

虽然JAVA和.NET都是实现W3C制定的WebService规范,但其实两者在实现上并没有完全保持一致,在实现SOAP规范1.1中JAX-WS并不需要SOAPAction,但.NET中是需要的,哪怕是空。这就导致了上面的异常。


那么SOAPAction到底是作什么的呢?

SOAPAction 被用来标识SOAP HTTP请求的目的地。

在.NET中的实现原理是这样的:默认情况下,.asmx 处理程序使用 SOAPAction 头的值来执行消息调度。当客户端调用WebService时,.asmx 处理程序会通过查看SOAPAction确定要调用哪个方法。如:

POST /math/math.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/Add" <soap:Envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <Add xmlns="http://tempuri.org/">       <x>33</x>       <y>66</y>     </Add>   </soap:Body> </soap:Envelope>
通过SOAPAction可以确定要调用Add方法。

SOAP信息中还有种路由方式那就是使用<Body>XML 元素之后的第一个子元素,如:

POST /math/math.asmx HTTP/1.1 Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "" <soap:Envelope  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   <soap:Body>     <Add xmlns="http://tempuri.org/">       <x>33</x>       <y>66</y>     </Add>   </soap:Body> </soap:Envelope>
当SOAPAction为空时,则使用<soap:Body> 第一个子元素加上其命名空间来路由:http://tempuri.org/Add。这就是上面的设置
RoutingStyle=SoapServiceRoutingStyle.RequestElement
SoapServiceRoutingStyle是枚举类型:
  1. SoapAction :SOAP 消息是根据 SOAPActionHTTP 标头路由的,默认值。
  2. RequestElement :SOAP 消息是根据 SOAP 消息中 <Body>XML 元素之后的第一个子元素路由的。

解决方案 
所以有两种解决方案可以处理这个异常:

  1. JAX-WS中加入SOAPAction并设置其值为空,可惜JAX-WS没提供相关设置API,但CFX、axis等开源框架提供了API;
  2. 设置.NET不使用SOAPAction,而使用<Body>XML 元素之后的第一个子元素做为路由
    RoutingStyle=SoapServiceRoutingStyle.RequestElement



0 0
原创粉丝点击