【问题标题】:add a soap header in C#在 C# 中添加一个肥皂头
【发布时间】:2015-02-05 11:04:51
【问题描述】:

我正在为我们使用第三方 Web 服务的 android 构建一个 Xamarin.forms 应用程序。我已经创建了代理,我可以看到服务公开的方法。

但我无法访问服务的方法,因为我必须在我的 SOAP 请求中添加标头,该请求需要密钥。

代码 sn-p:为代理创建客户端

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

我没有看到任何添加标头以便进行身份验证的选项。请指导我如何在我的请求中添加soap标头..

这在 android 应用程序中是可能的,因为他们正在创建附加标头并发送的 SOAP 对象。

示例标头 xml 请求:

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

我需要添加令牌、用户名和密码

【问题讨论】:

    标签: c# web-services xamarin xamarin.android xamarin.forms


    【解决方案1】:

    基本上你不能。您必须手动更改肥皂文本,例如:

     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】:

      通过覆盖 reference.cs 中的 System.Net.WebRequest 来修复它

      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;
      
          } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多