【问题标题】:Add wsse:UsernameToken in soap header在肥皂标题中添加 wsse:UsernameToken
【发布时间】:2013-01-21 09:21:12
【问题描述】:

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

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

<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>

我必须为它编写一个程序。

谁能帮帮我。

谢谢。

【问题讨论】:

    标签: java web-services soap soap-client


    【解决方案1】:

    这是我过去的肥皂程序。我已经根据您的情况对其进行了修改。

    //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 上搜索在 JDK 1.6 中使用 SOAP

    【讨论】:

    • 感谢您的回答。访问 url.response = connection.call(soapMessage, endpoint); 时出现未经授权的异常。我的凭据仅是正确的。你能告诉我是否需要添加任何额外的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多