add a soap header in C#

不问归期 提交于 2019-12-24 11:46:05

问题


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


回答1:


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();



回答2:


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;

    } 


来源:https://stackoverflow.com/questions/28342317/add-a-soap-header-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!