4

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).

Itai Sagi
  • 5,537
  • 12
  • 49
  • 73

3 Answers3

4

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.

Community
  • 1
  • 1
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

Here is an example on how to send authentication headers to a soap webservices: Authentication for Web Services (using SOAP headers)

Anas
  • 5,622
  • 5
  • 39
  • 71
  • If I get your answer, it's necessary to modify the service implementation, isn't it? What if you has no access to the services? – Alberto De Caro Jun 29 '12 at 15:33
  • @ADC You mean the server side? no the example just describe how to create a soap call (client side). – Anas Jun 29 '12 at 15:54
  • I don't agree. The suggested solution states: "To force the use of our new SOAP header we need to add the following attribute to our method:[...]". So it seems that the service has to be modified. – Alberto De Caro Jun 29 '12 at 16:02
  • We need to add soap header to authenticate for SoapClient. – cat916 Aug 03 '16 at 03:58
  • @AlbertoDeCaro no, you add the `[SoapHeader` annotation to the **client's** `[WebMethod`-annotated method, in the corresponding web service client stub. – rustyx Aug 22 '18 at 13:17
-1
        XmlDocument ReqDoc = new XmlDocument();
        XmlDocument doc = new XmlDocument();
            // request message
        ReqDoc.Load(@"D:\104Resqurst.xml");
           //adding soap headder 
        XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("soapenv", "Envelope", "http://www.w3.org/2001/XMLSchema-instance"));
        root.SetAttribute("xmlns", "http://mark.com/services/contracts");
        XmlElement header = (XmlElement)root.AppendChild(doc.CreateElement("soapenv", "Header", "http://www.w3.org/2001/XMLSchema-instance"));
        XmlElement body = (XmlElement)root.AppendChild(doc.CreateElement("soapenv", "Body", "http://www.w3.org/2001/XMLSchema-instance"));
       //assigning the request document to soap header doc
        doc.GetElementsByTagName("soapenv:Body")[0].InnerXml = ReqDoc.OuterXml;