2013-01-21 36 views
1

我正在使用SOAP客戶端。我的WSDL URL是http://localhost:8080/soap/getMessage?wsdl在肥皂標題中添加wsse:UsernameToken

這需要下列標題來指定用戶名和密碼。我不得不爲它編寫一個程序。

有人可以幫助我。

謝謝。

回答

2

這是我過去的肥皂計劃。我已經將其修改爲您的案例。

//create SOAP 
     SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); 
     SOAPConnection connection = sfc.createConnection(); 

     SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); 
     SOAPPart soapPart = soapMessage.getSOAPPart(); 
     SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 

     SOAPBody soapBody = soapEnvelope.getBody(); 
     SOAPElement Header = soapBody.addBodyElement(new QName("Header")); 

//attribute      
     SOAPElement Security= Header.addChildElement(new QName("Security")); 
     SOAPElement UsernameToken= Security.addChildElement(new QName("UsernameToken")); 
     SOAPElement Username= UsernameToken.addChildElement(new QName("Username")); 
     SOAPElement Password= UsernameToken.addChildElement(new QName("Password")); 

//enter the username and password 
Username.addTextNode("username"); 
Password.addTextNode("password"); 

//send the soap and print out the result 
URL endpoint = "http://localhost:8080/soap/getMessage?wsdl"; 
     SOAPMessage response = connection.call(soapMessage, endpoint); 


     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     String xml = ""; 
     try { 
      response.writeTo(out); 
      xml = out.toString("UTF-8"); 
     } catch (Exception e) 
     { 
      System.out.println(""+e); 
      //log.error(e.getMessage(),e); 
     }   

System.out.println(""+xml); 

進一步的信息,您可以搜索谷歌在JDK 1.6

+0

使用SOAP謝謝你的答案。當訪問url.response = connection.call(soapMessage,endpoint)時,我得到未經授權的異常。我的憑據只是正確的。你能告訴我需要添加額外的東西嗎? – Patan