C#SOAP服务从标题元素

时间:2015-07-02 13:15:20

标签: c# soap

我使用了服务引用从提供的WSDL生成SOAP客户端。除了导致服务失败的标题中的元素上的空白xmlns之外,一切都很好。使用SOAPUI,我知道如果我从元素中删除它,请求工作正常。如何以编程方式从属性中删除此空白xmlns?因为这是从外部应用程序加载的DLL,所以我没有使用app.config。

SecurityHeaderType是一个生成的对象,由h:Security元素及其对应的命名空间组成。在SecurityHeaderType中有一个名为Any的XmlElement [],这是我设置元素的地方。

private SecurityHeaderType GetSecurityHeaderType()
    {
        SecurityHeaderType securityHeader = new SecurityHeaderType();

        DateTime created = DateTime.Now;

        string creationDate;
        creationDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");

        string nonce = nonce = (new Random().Next(0, int.MaxValue)).ToString();

        byte[] hashedPassword;
        hashedPassword = GetSHA1(password);

        string concatednatedDigestInput = string.Concat(nonce, creationDate, Encoding.Default.GetString(hashedPassword));
        byte[] digest;
        digest = GetSHA1(concatednatedDigestInput);

        string passwordDigest;
        passwordDigest = Convert.ToBase64String(digest);

        string encodedNonce;
        encodedNonce = Convert.ToBase64String(Encoding.Default.GetBytes(nonce));

        XmlDocument doc = new XmlDocument();
        using (XmlWriter writer = doc.CreateNavigator().AppendChild())
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("Security");
            writer.WriteStartElement("UsernameToken");
            writer.WriteElementString("Username", username);
            writer.WriteElementString("Password", passwordDigest);
            writer.WriteElementString("Nonce", encodedNonce);
            writer.WriteElementString("Created", creationDate);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }

        doc.DocumentElement.RemoveAllAttributes();
        System.Xml.XmlElement[] headers = doc.DocumentElement.ChildNodes.Cast<XmlElement>().ToArray<XmlElement>();

        securityHeader.Any = headers;

        return securityHeader;


    }

当我在客户端上调用方法时,每个请求都必须具有上述标头,但它实际上生成了以下XML;

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
    <h:Security xmlns:h="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <UsernameToken xmlns="">
            <Username></Username>
            <Password></Password>
            <Nonce></Nonce>
            <Created></Created>
        </UsernameToken>
    </h:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <createShipmentRequest>
        <requestedShipment>
            <customerReference>Customer Ref</customerReference>
        </requestedShipment>
    </createShipmentRequest>
</s:Body>

我把它作为Security元素中的第一个元素并不重要,它总是自动出现xmlns =“”。

我想简单地得到它以便发出以下请求并且usernameToken元素没有空名称空间,我知道它可以正常工作;

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
    <h:Security xmlns:h="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <UsernameToken>
            <Username></Username>
            <Password></Password>
            <Nonce></Nonce>
            <Created></Created>
        </UsernameToken>
    </h:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <createShipmentRequest>
        <requestedShipment>
            <customerReference>Customer Ref</customerReference>
        </requestedShipment>
    </createShipmentRequest>
</s:Body>

1 个答案:

答案 0 :(得分:1)

您需要包含默认命名空间,因此:

writer.WriteStartElement("UsernameToken", 
    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

等等。 “正确”XML中的所有元素都具有此命名空间,因为它们从h:Security中的声明中指定的默认命名空间继承它:

<h:Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">