繁体   English   中英

如何从SOAP头获取头元素名称和值?

[英]How to get header elements name and value from SOAP header?

如何从soap请求标头获取用户标识和密码标记名称和值。

我的请求xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://WS.com/">
<soapenv:Header>
<userID>34</userID>
<password>test</password>
</soapenv:Header>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>

我的java代码

MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPPart  soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();

我想获得用户ID和密码来验证请求。

请帮忙

谢谢

尝试使用此代码:

// raw SOAP input as String
String input = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://WS.com/\">"
             + "<soapenv:Header>"
             + "<userID>34</userID>"
             + "<password>test</password>"
             + "</soapenv:Header>"
             + "<soapenv:Body>"
             + "</soapenv:Body>"
             + "</soapenv:Envelope>";

// Use MessageFactory with raw input as byte array
InputStream is = new ByteArrayInputStream(input.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, is);
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPHeader header = envelope.getHeader();

// obtain all Nodes tagged 'userID' or 'password'
NodeList userIdNode = header.getElementsByTagNameNS("*", "userID");
NodeList passwordNode = header.getElementsByTagNameNS("*", "password");

// extract the username and password
String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue();
String password = passwordNode.item(0).getChildNodes().item(0).getNodeValue();

System.out.println("userID: " + userId);
System.out.println("password: " + password);

输出:

userID: 34
password: test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM