java 在soap头中添加wsse:UsernameToken

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14435580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:15:55  来源:igfitidea点击:

Add wsse:UsernameToken in soap header

javaweb-servicessoapsoap-client

提问by Patan

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

我正在处理 SOAP 客户端。我的 WSDL URL 是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.

谢谢。

回答by goravine

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

有关更多信息,您可以搜索谷歌以在 JDK 1.6 中使用 SOAP