Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding SOAP Header to request

I have been trying to add a header to SOAP request as follows

<soapenv:Header>
     <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
     <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
     <SessionType xmlns="http://test.com/webservices">None</SessionType>
</soapenv:Header>

I have found suggestions to use SoapHeader to include header values, but introduces another level such as

<soapenv:Header>
    <CustomHeader>
        <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
        <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
        <SessionType xmlns="http://test.com/webservices">None</SessionType>
    </CustomHeader>
</soapenv:Header>

Can anyone suggest how I can form a request without CustomHeader.

like image 462
Anupdas Avatar asked May 23 '14 17:05

Anupdas


People also ask

What is header in SOAP request?

The SOAP header is an optional section in the SOAP envelope, although some WSDL files require that a SOAP header is passed with each request. A SOAP header contains application-specific context information (for example, security or encryption information) that is associated with the SOAP request or response message.

How do you add a security header to SOAP request in spring boot?

While using WebServiceTemplate, Spring provides numerous ways to intercept the request and modify the request and response. Hence, the interceptor can be a one way to add a header in the request. Similarly, we can implement WebServiceMessageCallback and override doWithMessage() method to add custom header.


1 Answers

Try to use this one

private static void Main()
{
    using (var client = new ServiceClient())
    using (var scope = new OperationContextScope(client.InnerChannel))
    {
        MessageHeader usernameTokenHeader = MessageHeader.CreateHeader("UsernameToken",
            "http://test.com/webservices", "username");
        OperationContext.Current.OutgoingMessageHeaders.Add(usernameTokenHeader);

        MessageHeader passwordTextHeader = MessageHeader.CreateHeader("PasswordText",
            "http://test.com/webservices", "password");
        OperationContext.Current.OutgoingMessageHeaders.Add(passwordTextHeader);

        MessageHeader sessionTypeHeader = MessageHeader.CreateHeader("SessionType",
            "http://test.com/webservices", "None");
        OperationContext.Current.OutgoingMessageHeaders.Add(sessionTypeHeader);

        string result = client.GetData(1);
        Console.WriteLine(result);
    }
    Console.ReadKey();
}

The Service Trace viewer shows following

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <UsernameToken xmlns="http://test.com/webservices">username</UsernameToken>
        <PasswordText xmlns="http://test.com/webservices">password</PasswordText>
        <SessionType xmlns="http://test.com/webservices">None</SessionType>
        <To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://localhost:13332/Service1.svc</To>
        <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetData</Action>
    </s:Header>
</s:Envelope>

Take a look OperationContextScope for more info

like image 65
GSerjo Avatar answered Sep 25 '22 01:09

GSerjo