[英]C# SOAP Service Remove xmlns=“” From Header Element
我已经使用服务参考从提供的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>
您需要包括默认名称空间,因此:
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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.