Axis2 服务器未能识别 HTTP 头 SOAPAction 的值 的解决办法

公司的WebService服务端使用.NET生成,客户端需要使用Java来写,客户端代码如下:

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import java.rmi.RemoteException;

public class Axis2Test
{
    public static void main(String[] args) throws RemoteException {
        test();
    }

    public static void test() throws AxisFault {
        try {
            String url = "目标URL";
            Options options = new Options();
            EndpointReference targetEPR = new EndpointReference(url);
            options.setTo(targetEPR);
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);
            OMFactory fac = OMAbstractFactory.getOMFactory();
            String tns = "目标的TargetNameSpace";
            OMNamespace omNs = fac.createOMNamespace(tns, "");
            OMElement method = fac.createOMElement("调用的方法名", omNs);
            OMElement symbol = fac.createOMElement("参数名", omNs);
            symbol.addChild(fac.createOMText(symbol, "参数值"));
            method.addChild(symbol);
            method.build();
            OMElement result = sender.sendReceive(method);
            System.out.println(result);
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

在运行这段代码时出现了如下错误:

org.apache.axis2.AxisFault: System.Web.Services.Protocols.SoapException: 服务器未能识别 HTTP 头 SOAPAction 的值: 。
   在 System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
   在 System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   在 System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   在 System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
   在 System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
    at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:508)
    at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:368)
    at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:414)
    at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
    at org.apache.axis2.client.OperationClient.execute(OperationClient.java:150)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:533)
    at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:509)
    at Axis2Test.test(Axis2Test.java:34)
    at Axis2Test.main(Axis2Test.java:15)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

经过查找之后发现JAX-WS规范不需要SoapAction,但是.NET需要,所以产生了这个错误。
在上网查找了之后发现可以在option对象下添加一个属性:

options.setAction("目标的TargetNameSpace"+"调用的方法名");
  • 1

添加后代码为:

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import java.rmi.RemoteException;

public class Axis2Test
{
    public static void main(String[] args) throws RemoteException {
        test();
    }

    public static void test() throws AxisFault {
        try {
            String url = "目标URL";
            Options options = new Options();
            EndpointReference targetEPR = new EndpointReference(url);
            options.setTo(targetEPR);
            options.setAction("目标的TargetNameSpace"+"调用的方法名");//需要加上这条语句
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);
            OMFactory fac = OMAbstractFactory.getOMFactory();
            String tns = "目标的TargetNameSpace";
            OMNamespace omNs = fac.createOMNamespace(tns, "");
            OMElement method = fac.createOMElement("调用的方法名", omNs);
            OMElement symbol = fac.createOMElement("参数名", omNs);
            symbol.addChild(fac.createOMText(symbol, "参数值"));
            method.addChild(symbol);
            method.build();
            OMElement result = sender.sendReceive(method);
            System.out.println(result);
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值