Add wsse:UsernameToken in soap header

巧了我就是萌 提交于 2019-12-10 11:42:15

问题


I am working on SOAP client. My WSDL URL is http://localhost:8080/soap/getMessage?wsdl.

This requires the the following header to specify the username and password.

<wsdl:Envelope xmlns:soap="..."
        xmlns:wsse="..." >
       <wsdl:Header>
       <wsse:Security>
       <wsse:UsernameToken>
       <wsse:Username>admin</wsse:Username>
       <wsse:Password>password</wsse:Password>
       </wsse:UsernameToken>
       </wsse:Security>
       </wsdl:Header>
</wsdl:Envelope>

I have to write a program for it.

Can some one help me.

Thanks.


回答1:


here is my past program for soap. I already modified it to your case.

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

for further information you can search the google for using SOAP in JDK 1.6



来源:https://stackoverflow.com/questions/14435580/add-wsseusernametoken-in-soap-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!