简体   繁体   中英

How to define XML namespaces to be incuded in the header of XML request which will be send via invoke of a Web Service call?

I have developed a C# Class in VS 2012 from which I have to call (consume) through HTTPS, a remote web service method. I have already apply code to create custom headers for security tag, however I must apply in the root a declaration of namespace like

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

The complete XML request that must be send through web service method invoke will be ( and has been tested successfully with SOAP UI ) as of the following :

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <env:Header>
        <ns1:Security>
            <ns1:UsernameToken>
                <ns1:Username>XXXXXXXXXXXXXXXXXXXX</ns1:Username>
                <ns1:Password>ZZZZZZZZZZZZZZZZZZZZ</ns1:Password>
            </ns1:UsernameToken>
        </ns1:Security>
    </env:Header>
    <env:Body>
        <ns:vhWsVersion/>
    </env:Body>
</env:Envelope>

for this, to work the namespace

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

must be include via the invoke of web service method.

Any assistance of how to achieve this is kindly appreciated

Edit

var customBinding = new CustomBinding(); 
customBinding.Elements.Add(new TextMessageEncodingBindingElement 
{ 
   MessageVersion = MessageVersion.Soap11, 
   WriteEncoding = System.Text.Encoding.UTF8
}); 
var Uri = new Uri("https://"); 
var endpointAddres = new EndpointAddress(Uri, new MySecurityHeader()); 
var client = new ChannelFactory<ServiceReference3.VhWsCreatePayId>(customBinding)
             .CreateChannel(e‌​ndpointAddres); 
client.vhWsCreatePayIdVersion(request);

You'll need to ensure that the http://docs.oasis-open.org/... Namespace is included in the SoapHeader , eg

[System.Xml.Serialization.XmlRootAttribute(Namespace = 
  "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
   IsNullable = false)]
public partial class UsernameToken : System.Web.Services.Protocols.SoapHeader
{
    // Namespace is also available here if different from the root element.
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Username {get; set;}
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string Password {get; set;}
}

Edit
If you are using another technique to build the SoapHeader , note that the oasis namespace doesn't necessarily need to go into the root Envelope element - it can be placed locally in the header, eg:

<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <UsernameToken>
        <Username>XXXXXXXXXXXXXXXXXXXX</Username>
        <Password>ZZZZZZZZZZZZZZZZZZZZ</Password>
    <UsernameToken>
</Security>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM