Java 使用 Axis 1.4 设置自定义 SOAP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/786488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set custom SOAP header using Axis 1.4
提问by razenha
I'm trying to consume a .NET 2.0 web service using Axis. I generated the web services client using Eclipse WST Plugin and it seems ok so far.
我正在尝试使用 Axis 使用 .NET 2.0 Web 服务。我使用 Eclipse WST 插件生成了 Web 服务客户端,到目前为止看起来还可以。
Here the expected SOAP header:
这里是预期的 SOAP 标头:
<soap:Header>
<Authentication xmlns="http://mc1.com.br/">
<User>string</User>
<Password>string</Password>
</Authentication>
</soap:Header>
I didn't find any documentation on how to configure this header from an Axis client.
When I generated the client using Visual Studio C# Express 2008, it generates a class named Authentication
with two String attributes (User
and Password
) and all the client methods receive an object of this class as first parameter, but it did not happen with Axis WS client.
我没有找到有关如何从 Axis 客户端配置此标头的任何文档。当我使用 Visual Studio C# Express 2008 生成客户端时,它生成了一个以Authentication
两个 String 属性 ( User
and Password
)命名的类,并且所有客户端方法都接收一个此类的对象作为第一个参数,但 Axis WS 客户端没有发生这种情况。
How can I set this header in my client calls?
如何在客户端调用中设置此标头?
采纳答案by martsraits
Maybe you can use org.apache.axis.client.Stub.setHeader
method? Something like this:
也许你可以使用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...
回答by Joe Zitzelberger
If you have an object representing the Authentication
container with userid and password, you can do it like so:
如果您有一个对象代表Authentication
带有用户名和密码的容器,您可以这样做:
import org.apache.axis.client.Stub;
//...
MyAuthObj authObj = new MyAuthObj("userid","password");
((Stub) yourServiceObject).setHeader("urn://your/name/space/here", "partName", authObj);
回答by ammu
I have the same issue and solved by the below fragement:
我有同样的问题并通过以下片段解决:
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);