首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在soap标头中添加wsse:UsernameToken

在soap标头中添加wsse:UsernameToken
EN

Stack Overflow用户
提问于 2013-01-21 17:21:13
回答 1查看 13.7K关注 0票数 2

我正在做SOAP客户端的工作。我的WSDL URL是http://localhost:8080/soap/getMessage?wsdl

这需要以下标头来指定用户名和密码。

代码语言:javascript
复制
<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>

我必须为此写一个程序。

谁能帮帮我。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-01-21 18:26:33

这是我以前编写的soap程序。我已经把它改成你的案子了。

代码语言:javascript
复制
//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);

有关更多信息,您可以在google上搜索在JDK1.6中使用SOAP

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14435580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档