【问题标题】:Set custom SOAP header using Axis 1.4使用 Axis 1.4 设置自定义 SOAP 标头
【发布时间】:2009-04-24 15:58:56
【问题描述】:

我正在尝试使用 Axis 使用 .NET 2.0 Web 服务。 我使用 Eclipse WST 插件生成了 Web 服务客户端,目前看来还可以。

这里是预期的 SOAP 标头:

<soap:Header>
<Authentication xmlns="http://mc1.com.br/">
    <User>string</User>
    <Password>string</Password>
</Authentication>
</soap:Header>

我没有找到任何关于如何从 Axis 客户端配置此标头的文档。 当我使用 Visual Studio C# Express 2008 生成客户端时,它会生成一个名为 Authentication 的类,该类具有两个字符串属性(User 和 Password),并且所有客户端方法都接收此类的对象作为第一个参数,但它Axis WS 客户端没有发生这种情况。

如何在我的客户端调用中设置此标头?

【问题讨论】:

    标签: java .net web-services soap axis


    【解决方案1】:

    也许你可以使用org.apache.axis.client.Stub.setHeader 方法?像这样的:

    MyServiceLocator wsLocator = new MyServiceLocator();
    MyServiceSoap ws = wsLocator.getMyServiceSoap(new URL("http://localhost/MyService.asmx"));
    
    //add SOAP header for authentication
    SOAPHeaderElement authentication = new SOAPHeaderElement("http://mc1.com.br/","Authentication");
    SOAPHeaderElement user = new SOAPHeaderElement("http://mc1.com.br/","User", "string");
    SOAPHeaderElement password = new SOAPHeaderElement("http://mc1.com.br/","Password", "string");
    authentication.addChild(user);
    authentication.addChild(password);
    ((Stub)ws).setHeader(authentication);
    
    //now you can use ws to invoke web services...
    

    【讨论】:

    • 经过这么多令人沮丧的小时后,这就是我需要的答案。谢谢
    • 这正是我所需要的!
    • SOAPHeaderElement authentication = new SOAPHeaderElement("http://mc1.com.br/","Authentication"); 这个语句给了我和错误cannot initiate abstract class java 如何解决这个问题?
    • 如果 header 有 2 个这样的 soapElement 怎么办:
    【解决方案2】:

    如果你有一个代表Authentication容器的对象,带有用户名和密码,你可以这样做:

    import org.apache.axis.client.Stub;
    
    //...
    
    MyAuthObj authObj = new MyAuthObj("userid","password");
    ((Stub) yourServiceObject).setHeader("urn://your/name/space/here", "partName", authObj);
    

    【讨论】:

    • MyAuthObj 是哪种对象?也许 OP 的那个是Authentication?如果是这样,客户端如何实例化这样的对象?...谢谢!
    【解决方案3】:

    我有同样的问题并通过以下片段解决:

    ServiceSoapStub clientStub = (ServiceSoapStub)new ServiceLocator().getServiceSoap(url);
    org.apache.axis.message.SOAPHeaderElement header = new org.apache.axis.message.SOAPHeaderElement("http://www.abc.com/SSsample/","AuthHeader");
    SOAPElement node = header.addChildElement("Username");
    node.addTextNode("aat");
    SOAPElement node2 = header.addChildElement("Password");
    node2.addTextNode("sd6890");
    
    ((ServiceSoapStub) clientStub).setHeader(header);
    

    【讨论】:

      【解决方案4】:

      我使用了与之前提到的几乎相同的解决方案:

      SOAPHeaderElement cigWsHeader = new SOAPHeaderElement("https://ws.creditinfo.com", "CigWsHeader");
      cigWsHeader.addChildElement("用户名").setValue("用户名");
      cigWsHeader.addChildElement("密码").setValue("密码");
      
      var port = new ServiceLocator().getServiceSoap(someUrl);
      ((Stub) 端口).setHeader(cigWsHeader);

      并花费数小时搜索“找不到安全标头”的原因。答案是......命名空间中的冗余符号“/”:“https://ws.creditinfo.com/”。严重地!记住这一点。

      【讨论】:

        猜你喜欢
        • 2012-12-16
        • 1970-01-01
        • 2013-09-18
        • 2014-09-20
        • 2016-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-09
        相关资源
        最近更新 更多