Adding custom SoapClient headers

Itai Sagi picture Itai Sagi · Jun 3, 2012 · Viewed 12k times · Source

I'm a complete C# newbie and have been trying to crack this for a couple of hours now with no success...

I need to build a SoapClient to use on C#... I've been trying to port an existing php client to c#.

basically: I need to make a request with a Soap header which contains user and password, this is an example of xml I should be sending.

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.paymentmanager.mycheck.com">
    <soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <wsse:UsernameToken>
                <wsse:Username>test</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <SOAP-ENV:Body>
        <ns1:testws>
            <ns1:x>1</ns1:x>
        </ns1:testws>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I used Visual Studio 'Add Service Reference...' When I do through

The service is working well, as using php it works brilliantly.

the C# I have concieved:

namespace ConsoleApplication1
{
    class Program
    {
        const String VENDOR_ID = "8723";
        const String VENDOR_PASS = "test";
        const String VENDOR_USER = "pass";
        static void Main(string[] args)
        {
            try
            {
                PaymentManager.PaymentManagerPortTypeClient test = new PaymentManager.PaymentManagerPortTypeClient();
                int num = test.testws(5);
                Console.WriteLine(num);
            }
            catch( Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }

}

obviously, I didn't know how to implement the Soap headers, so it throws a 'Missing SOAP Headers' Exception (which is recieved from the WebService).

Answer

Alberto De Caro picture Alberto De Caro · Jul 2, 2012

I had the same problem. I found no solution. Eventually I had to create the whole SOAP message and to send it via an HTTPWebRequest.

Have a look here.