简体   繁体   中英

add a soap header in C#

I am building an Xamarin.forms App for android where we are consuming third party webservices. I have created the proxy and I can see the methods exposed by the service.

But I cannot access the methods of the service as I have to add header to my SOAP request which takes the key.

Code snippet: created client for the proxy

ThirdPartAuthService.AuthService clnt = new ThirdPartAuthService.AuthService();
clnt.getenquiry(XML);

I do not see any option to add header so that authentication happens. Please guide me how to add soap header to my request..

Its possible in android app as they are creating SOAP object appending the header and sending.

Sample header xml request:

<?xml version="1.0" encoding="utf-8"?>...
    <soapenv:Header><ns1:encKey soapenv:actor="http://schemas.xmlsoap.org/
    soap/actor/next"  xsi:type="soapenc:string" xmlns:ns1=
     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
    abcde</ns1:encKey></soapenv:Header>..

I need to add token, username and password

Basically you can't. You have to change Soap text manually like:

 String soapBodyString = getXMLFromCache ();

                int pos1 = soapBodyString.IndexOf ("<soap:Body");  
                int pos2 = soapBodyString.Length - pos1;

                soapBodyString = soapBodyString.Substring (pos1, pos2);

                string headerText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                    + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n{0}>"
                    + "<soapenv:Header><wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
                    + "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"
                    + "<wsse:Username>{1}</wsse:Username>"
                    + "<wsse:Password>{2}</wsse:Password>"
                    + "<wsse:Signature>{3}</wsse:Signature>"
                    + "</wsse:UsernameToken></wsse:Security></soapenv:Header>";


                Stream appOutputStream = new MemoryStream ();
                StreamWriter soapMessageWriter = new StreamWriter (appOutputStream);

                    headerText = string.Format (headerText,UriNamespace ,Username, Password, Signature);
                soapMessageWriter.Write (headerText);
                soapBodyString=soapBodyString.Replace("soap:Envelope", "soapenv:Envelope");
                soapBodyString=soapBodyString.Replace("soap:Body", "soapenv:Body");
                soapMessageWriter.Write(soapBodyString);

                soapMessageWriter.Flush();
                appOutputStream.Flush();
                appOutputStream.Position = 0;

                StreamReader reader = new StreamReader(appOutputStream);
                StreamWriter writer = new StreamWriter(this.outputStream);
                writer.Write(reader.ReadToEnd());
                writer.Flush();
                appOutputStream.Close();

Fixed it by overriding System.Net.WebRequest in reference.cs

protected override System.Net.WebRequest GetWebRequest(Uri uri)
    { 

        HttpWebRequest request;
        request = (HttpWebRequest)base.GetWebRequest(uri);
        NetworkCredential networkCredentials =   Credentials.GetCredential(uri, "Basic");

        //Other credentials  

        return request;

    } 

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