如何从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和密码来验证请求。
请帮忙
由于
答案 0 :(得分:1)
尝试使用此代码:
// 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