[Axis2]incorrect content-type for SOAP version

在Axis2测试中遇到SOAP版本Content-Type异常,HTTP请求头Content-Type字段设置不正确,导致问题。分析发现Axis2自动生成的代码中设置SOAP版本的部分影响了HTTP请求头。未设置时,默认使用SOAP 1.1,而HTTP响应头中的Content-Type同样关键。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在测试的过程中遇到了一个奇怪的异常:

com.sun.xml.internal.messaging.saaj.soap.SOAPVersionMismatchException: 
		Cannot create message: incorrect content-type for SOAP version.
		Got: application/soap+xml; 
				action="http://smarttrust.com/opm/ws/opm/ActivateRequest"; 
				charset=UTF-8 
		Expected: text/xml

通过异常信息,可以很明显的发现是携带SOAP消息的HTTP报文请求头的Content-Type字段出了问题

通过抓包可以看到HTTP报文请求头的格式是下面这个样子的(还要注意的一点是,没有SOAPAction字段,而是action字段

POST /opm HTTP/1.1
Content-Type: application/soap+xml; charset=UTF-8; 
action="http://smarttrust.com/opm/ws/opm/ActivateRequest"
User-Agent: Axis2
Host: 10.160.68.107:9010
Transfer-Encoding: chunked

之后我新建了一个测试工程,仔细的查看了下Axis2自动生成的代码,发现有这么一段:

public MathStub(
		org.apache.axis2.context.ConfigurationContext configurationContext,
		java.lang.String targetEndpoint, boolean useSeparateListener)
		throws org.apache.axis2.AxisFault {
	populateAxisService();
	populateFaults();
	_serviceClient = new org.apache.axis2.client.ServiceClient(
			configurationContext, _service);
	_serviceClient.getOptions().setTo(
			new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
	_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
	_serviceClient.getOptions().setSoapVersionURI(
			org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
}

这部分是用来设置SOAP版本的

_serviceClient.getOptions().setSoapVersionURI(
		org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

如果使用SOAP11Constants(SOAP 1.1),HTTP请求头如下:

POST /test HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "urn:add"
User-Agent: Axis2
Host: 146.11.42.87:9876
Transfer-Encoding: chunked

如果使用SOAP12Constants(SOAP 1.2) ,HTTP请求头如下:

POST /test HTTP/1.1
Content-Type: application/soap+xml; charset=UTF-8; action="urn:add"
User-Agent: Axis2
Host: 146.11.42.87:9876
Transfer-Encoding: chunked

如果不设置这个参数,HTTP请求头如下(和SOAP 1.1是一样的,可以确认默认使用SOAP 1.1):

POST /test HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "urn:add"
User-Agent: Axis2
Host: 146.11.42.87:9876
Transfer-Encoding: chunked

HTTP响应头中的Content-Type字段也是一样的

参考:

http://stackoverflow.com/questions/8092031/cannot-create-message-incorrect-content-type-for-soap-version-got-text-xml-ch

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值