ksoap2的安全标头错误

时间:2013-02-19 22:53:32

标签: android wsdl ksoap2 android-ksoap2

尝试连接到.Net Web服务时出现以下错误

  

SoapFault - faultcode:'q0:Security'faultstring:'Security   不满足要求,因为安全标头不是   出现在收到的消息中。'

以下是服务器端的代码,用于创建安全标头

        public override XmlElement GetXml(XmlDocument document) {
        if (null == document) throw new ArgumentException("document");

        XmlElement root = document.CreateElement("abc", "TokenName", "http://testurl.com");

        if (!string.IsNullOrEmpty(Id)) {
            root.SetAttribute(WSUtility.Prefix, WSUtility.NamespaceURI);
            root.SetAttribute(WSUtility.AttributeNames.Id, WSUtility.NamespaceURI, Id);
        }

        XmlElement machineIdElement = document.CreateElement("abc", "machineId", "http://testurl.com");

        machineIdElement.InnerText = "060a5270-7ae7-11e2-b92a-0800200c9a66";

        root.AppendChild(machineIdElement);

        XmlElement inspectorIdElement = document.CreateElement("dac", "insId", "http://testurl.com");

        inspectorIdElement.InnerText = "dc0a5270-7ae7-11e2-b92a-0800200c9a66";

        root.AppendChild(inspectorIdElement);

        return root;
    }

有人可以告诉我如何根据上面的代码为ksoap2创建安全头。 感谢任何帮助

提前致谢 史蒂夫

1 个答案:

答案 0 :(得分:0)

试试这个......

// create header
        Element[] header = new Element[1];
        header[0] = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security");
        header[0].setAttribute(null, "mustUnderstand","1");

        Element usernametoken = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "UsernameToken");
        usernametoken.setAttribute(null, "Id", "UsernameToken-1");
        header[0].addChild(Node.ELEMENT,usernametoken);

        Element username = new Element().createElement(null, "n0:Username");
        username.addChild(Node.IGNORABLE_WHITESPACE,"AJAY");
        usernametoken.addChild(Node.ELEMENT,username);

        Element pass = new Element().createElement(null,"n0:Password");
        pass.setAttribute(null, "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        pass.addChild(Node.TEXT, "hello");
        usernametoken.addChild(Node.ELEMENT, pass);