2013-02-03 183 views
0

我們正在實現一個需要在SOAP頭元素中檢索用戶名和密碼並將其用於進一步使用/處理的JAX-WS Web服務。 當我們檢索用戶名/密碼時,它會變爲空。請幫忙。SOAP消息檢索

if (Boolean.FALSE.equals(context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY))) {  
      try { 
       SOAPMessage sm = context.getMessage(); 
       //SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope(); 
       SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope(); 
       SOAPHeader sh = envelope.getHeader(); 

       System.out.println("Message: "+envelope); 
       System.out.println("Envelope: "+envelope); 
       System.out.println("Header: "+sh.toString()); 
       Iterator it = sh.examineAllHeaderElements(); 
       while(it.hasNext()){ 
        System.out.println(it.next()); 
       } 

      String username; 
      username = sh.getAttribute("Username"); 
      // username = sh.getAttributeValue("Username"); 
      //String password = sh.getAttribute("Password"); 
       System.out.println("uid:"+username); 
       //System.out.println("pass: "+password); 
       context.put("Username", username); 
       //context.put("Passsword", password); 
       // default scope is HANDLER (i.e., not readable by SEI 
       // implementation) 
       context.setScope("Username", MessageContext.Scope.APPLICATION); 

回答

2

試試這個:

public boolean handleMessage(SOAPMessageContext context) { 
    try { 
     SOAPMessage message = context.getMessage(); 
     SOAPHeader header = message.getSOAPHeader();    
     if (header != null) { 
      Iterator i = header.getChildElements(); 
      //Navigate through header elements to get the username and password 
     } 
    } catch (Exception e) { 
     //Handle exception 
    } 
    return true; 
} 

參見:

+0

樣品 TestUser TestPassword user2036302

+0

然後,您的郵件根本不包含標頭。你確定這個標題是它應該在哪裏? –

+0

實際上,用戶名和密碼不是soap頭元素的屬性(它們是嵌套元素)。 – user2036302