context: /* 
  XML-RPC.NET library
  Copyright (c) 2001-2011, Charles Cook <charlescook@cookcomputing.com>
  
  Permission is hereby granted, free of charge, to any person 
  obtaining a copy of this software and associated documentation 
  files (the "Software"), to deal in the Software without restriction, 
  including without limitation the rights to use, copy, modify, merge, 
  publish, distribute, sublicense, and/or sell copies of the Software, 
  and to permit persons to whom the Software is furnished to do so, 
  subject to the following conditions:
  
  The above copyright notice and this permission notice shall be 
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
  DEALINGS IN THE SOFTWARE.
  */
  
  using System;
  using System.ComponentModel;
  using System.Collections;
  using System.Diagnostics;
  using System.IO;
  using System.Net;
  using System.Reflection;
  using System.Security.Cryptography.X509Certificates;
  using System.Text;
  
  namespace CookComputing.XmlRpc
  {
    public class XmlRpcClientProtocol : 
  #if (!SILVERLIGHT)
      Component,
  #endif
      IXmlRpcProxy
    {
      WebSettings webSettings = new WebSettings();
      XmlRpcFormatSettings XmlRpcFormatSettings = new XmlRpcFormatSettings();
  
      private CookieCollection _responseCookies;
      private WebHeaderCollection _responseHeaders;
      private XmlRpcNonStandard _nonStandard;
      private string _url = null;
      private string _xmlRpcMethod = null;
  
      private Guid _id = Util.NewGuid();
  
      static XmlRpcClientProtocol()
      {
  #if (SILVERLIGHT)
        WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
        WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);
  #endif
      }
  
  #if (!COMPACT_FRAMEWORK && !SILVERLIGHT)
      public XmlRpcClientProtocol(System.ComponentModel.IContainer container)
      {
        container.Add(this);
        InitializeComponent();
      }
  #endif
      public XmlRpcClientProtocol()
      {
        InitializeComponent();
      }
  
      public object Invoke(
        MethodBase mb,
        params object[] Parameters)
      {
        return Invoke(this, mb as MethodInfo, Parameters);
      }
  
      public object Invoke(
        MethodInfo mi,
        params object[] Parameters)
      {
        return Invoke(this, mi, Parameters);
      }
  
      public object Invoke(
        string MethodName,
        params object[] Parameters)
      {
        return Invoke(this, MethodName, Parameters);
      }
  
      public object Invoke(
        Object clientObj,
        string methodName,
        params object[] parameters)
      {
        MethodInfo mi = GetMethodInfoFromName(clientObj, methodName, parameters);
        return Invoke(this, mi, parameters);
      }
  
      public object Invoke(
        Object clientObj,
        MethodInfo mi,
        params object[] parameters)
      {
  #if (SILVERLIGHT)
        throw new NotSupportedException();
  #else
  
        _responseHeaders = null;
        _responseCookies = null;
        WebRequest webReq = null;
        object reto = null;
        try
        {
          string useUrl = GetEffectiveUrl(clientObj);
          webReq = GetWebRequest(new Uri(useUrl));
          XmlRpcRequest req = MakeXmlRpcRequest(webReq, mi, parameters,
            clientObj, _xmlRpcMethod, _id);
          SetProperties(webReq);
          SetRequestHeaders(Headers, webReq);
  #if (!COMPACT_FRAMEWORK)
          SetClientCertificates(ClientCertificates, webReq);
  #endif
          Stream serStream = null;
          Stream reqStream = null;
          bool logging = (RequestEvent != null);
          if (!logging)
            serStream = reqStream = webReq.GetRequestStream();
          else
            serStream = new MemoryStream(2000);
          try
          {
            var serializer = new XmlRpcRequestSerializer(XmlRpcFormatSettings);
            serializer.SerializeRequest(serStream, req);
            if (logging)
            {
              reqStream = webReq.GetRequestStream();
              serStream.Position = 0;
              Util.CopyStream(serStream, reqStream);
              reqStream.Flush();
              serStream.Position = 0;
              OnRequest(new XmlRpcRequestEventArgs(req.proxyId, req.number,
                serStream));
            }
          }
          finally
          {
            if (reqStream != null)
              reqStream.Close();
          }
          HttpWebResponse webResp = GetWebResponse(webReq) as HttpWebResponse;
          _responseCookies = webResp.Cookies;
          _responseHeaders = webResp.Headers;
          Stream respStm = null;
          Stream deserStream;
          logging = (ResponseEvent != null);
          try
          {
            respStm = webResp.GetResponseStream();
  #if (!COMPACT_FRAMEWORK && !FX1_0)
            respStm = MaybeDecompressStream((HttpWebResponse)webResp,
              respStm);
  #endif
            if (!logging)
            {
              deserStream = respStm;
            }
            else
            {
              deserStream = new MemoryStream(2000);
              Util.CopyStream(respStm, deserStream);
              deserStream.Flush();
              deserStream.Position = 0;
            }
            if (logging)
            {
              OnResponse(new XmlRpcResponseEventArgs(req.proxyId, req.number,
                deserStream));
              deserStream.Position = 0;
            }
            XmlRpcResponse resp = ReadResponse(req, webResp, deserStream);
            reto = resp.retVal;
          }
          finally
          {
            if (respStm != null)
              respStm.Close();
          }
        }
        finally
        {
          if (webReq != null)
            webReq = null;
        }
        return reto;
  #endif
      }
  
      public bool AllowAutoRedirect
      {
        get { return webSettings.AllowAutoRedirect; }
        set { webSettings.AllowAutoRedirect = value; }
      }
  
  #if (!COMPACT_FRAMEWORK && !SILVERLIGHT)
      [Browsable(false)]
      public X509CertificateCollection ClientCertificates
      {
        get { return webSettings.ClientCertificates; }
      }
  #endif
  
  #if (!COMPACT_FRAMEWORK)
      public string ConnectionGroupName
      {
        get { return webSettings.ConnectionGroupName; }
        set { webSettings.ConnectionGroupName = value; }
      }
  #endif
  
      [Browsable(false)]
      public ICredentials Credentials
      {
        get { return webSettings.Credentials; }
        set { webSettings.Credentials = value; }
      }
  
  #if (!COMPACT_FRAMEWORK && !FX1_0)
      public bool EnableCompression
      {
        get { return webSettings.EnableCompression; }
        set { webSettings.EnableCompression = value; }
      }
  #endif
  
      [Browsable(false)]
      public WebHeaderCollection Headers
      {
        get { return webSettings.Headers; }
      }
  
  #if (!COMPACT_FRAMEWORK && !FX1_0 && !SILVERLIGHT)
      public bool Expect100Continue
      {
        get { return webSettings.Expect100Continue; }
        set { webSettings.Expect100Continue = value; }
      }
  
      public bool UseNagleAlgorithm
      {
        get { return webSettings.UseNagleAlgorithm; }
        set { webSettings.UseNagleAlgorithm = value; }
      }
  #endif
  
      public CookieContainer CookieContainer
      {
        get { return webSettings.CookieContainer; }
      }
  
      public Guid Id
      {
        get { return _id; }
      }
  
      public int Indentation
      {
        get { return XmlRpcFormatSettings.Indentation; }
        set { XmlRpcFormatSettings.Indentation = value; }
      }
  
      public bool KeepAlive
      {
        get { return webSettings.KeepAlive; }
        set { webSettings.KeepAlive = value; }
      }
  
      public XmlRpcNonStandard NonStandard
      {
        get { return _nonStandard; }
        set { _nonStandard = value; }
      }
  
      public bool PreAuthenticate
      {
        get { return webSettings.PreAuthenticate; }
        set { webSettings.PreAuthenticate = value; }
      }
  
  #if (!SILVERLIGHT)
      [Browsable(false)]
      public System.Version ProtocolVersion
      {
        get { return webSettings.ProtocolVersion; }
        set { webSettings.ProtocolVersion = value; }
      }
  #endif
  
  #if (!SILVERLIGHT)
      [Browsable(false)]
      public IWebProxy Proxy
      {
        get { return webSettings.Proxy; }
        set { webSettings.Proxy = value; }
      }
  #endif
  
      public CookieCollection ResponseCookies
      {
        get { return _responseCookies; }
      }
  
      public WebHeaderCollection ResponseHeaders
      {
        get { return _responseHeaders; }
      }
  
      public int Timeout
      {
        get { return webSettings.Timeout; }
        set { webSettings.Timeout = value; }
      }
  
      public string Url
      {
  
gt:       get { return _url; }