سؤال

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

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

        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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top