4

How can I get userID and password tag name and value from soap request Header.

My request 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>

My java code

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

i want to get userID and password to validate the request.

Please help

Thanks

haja
  • 135
  • 1
  • 4
  • 12

1 Answers1

2

Try using this code:

// 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);

Output:

userID: 34
password: test
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • i get "java.lang.NullPointerException" at the line String userId = userIdNode.item(0).getChildNodes().item(0).getNodeValue(); – haja Aug 24 '15 at 04:38
  • i still get "java.lang.NullPointerException" at the line String userId = userIdNode.item(0).getNodeValue(); What is the java import to use for NodeList ? – haja Aug 24 '15 at 04:45
  • i still get "java.lang.NullPointerException" at the line String userId = userIdNode.item(0).getNodeValue(); after importing org.w3c.dom.NodeList – haja Aug 24 '15 at 04:50
  • 1
    System.out.println(userIdNode.getLength()) prints 0(zero) – haja Aug 24 '15 at 04:54
  • i just use the above code to get the headers, please help if i'm wrong. – haja Aug 24 '15 at 04:59
  • You were never correctly initializing the `MessageFactory` with your SOAP content. My answer deliberately does this, but you are free to change it later on as you see fit. – Tim Biegeleisen Aug 24 '15 at 05:03
  • I still get the userID: null and password: null in the System.out.println("userID: " + userId); and System.out.println("password: " + password); – haja Aug 24 '15 at 05:07
  • the prolem came from SOAPHeader header which is null alwayse, did you fix the issue? – Selma BA Oct 07 '19 at 13:27