Ejemplo n.º 1
0
        /// <summary>
        /// Call an XmlRpc method
        /// </summary>
        /// <param name="methodName">method name</param>
        /// <param name="parameters">variable length list of parameters</param>
        /// <returns>response from the remote host</returns>
        /// <exception cref="Exception">allows all exceptions to propagate out of the method</exception>
        public XmlRpcMethodResponse CallMethod(string methodName, params XmlRpcValue[] parameters)
        {
            //select the encoding
            Encoding encodingToUse = StringHelper.GetEncoding(_transportEncoding, new UTF8Encoding(false, false));

            // build the XmlRpc packet
            byte[] requestBytes = GetRequestBytes(encodingToUse, methodName, parameters, false);

            if (ApplicationDiagnostics.VerboseLogging)
            {
                LogXmlRpcRequest(encodingToUse, methodName, parameters);
            }

            // send the request
            HttpWebResponse response;

            try
            {
                response = HttpRequestHelper.SendRequest(_hostname, delegate(HttpWebRequest request)
                {
                    request.Method            = "POST";
                    request.AllowAutoRedirect = false;
                    request.ContentType       = String.Format(CultureInfo.InvariantCulture, "{0};charset={1}", MimeHelper.TEXT_XML, encodingToUse.WebName);
                    if (_requestFilter != null)
                    {
                        _requestFilter(request);
                    }
                    using (Stream requestStream = request.GetRequestStream())
                        StreamHelper.Transfer(new MemoryStream(requestBytes), requestStream);
                });
            }
            catch
            {
                if (!ApplicationDiagnostics.VerboseLogging)  // if test mode, request has already been logged
                {
                    LogXmlRpcRequest(encodingToUse, methodName, parameters);
                }

                throw;
            }

            // WinLive 616: The response encoding may not necessarily be the same as our request encoding. Attempt to
            // use the encoding specified in the HTTP header.
            string characterSet;

            if (TryGetCharacterSet(response, out characterSet))
            {
                encodingToUse = StringHelper.GetEncoding(characterSet, encodingToUse);
            }

            // return the response
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), encodingToUse))
            {
                string xmlRpcString = reader.ReadToEnd();

                if (ApplicationDiagnostics.VerboseLogging)
                {
                    LogXmlRpcResponse(xmlRpcString);
                }

                try
                {
                    XmlRpcMethodResponse xmlRpcResponse = new XmlRpcMethodResponse(xmlRpcString);
                    if (xmlRpcResponse.FaultOccurred)
                    {
                        if (!ApplicationDiagnostics.VerboseLogging)  // if test mode, response has already been logged
                        {
                            LogXmlRpcRequest(encodingToUse, methodName, parameters);
                            LogXmlRpcResponse(xmlRpcString);
                        }
                    }
                    return(xmlRpcResponse);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("Exception parsing XML-RPC response:\r\n\r\n" + ex.ToString() + "\r\n\r\n" + xmlRpcString);
                    throw;
                }
            }
        }