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


posted @ 2012-09-03 11:18  码农神说  阅读(1045)  评论(0编辑  收藏  举报