Return to Snippet

Revision: 33643
at October 11, 2010 20:28 by dzr_gregory


Initial Code
/// <summary>
        /// Execute XML-RPC request and returns XML response as string
        /// </summary>
        /// <param name="uri">URL for request, for examle: "http://snipplr.com/xml-rpc.php"</param>
        /// <param name="methodName">Name of method to be called, for example: snippet.list</param>
        /// <param name="parameters">Optional parameters for the method. 
        /// Can be null or must have members of following types: short, int, bool, double, string, DateTime
        /// </param>
        /// <returns>String than contains XML response of method</returns>
        public static string XmlRpcExecMethod(string uri, string methodName, List<Object> parameters)
        {
            var req = (HttpWebRequest)WebRequest.Create(uri);
            req.Method = "POST";
            var genParams = "";
            if (parameters != null && parameters.Count > 0)
            {
                foreach (var param in parameters)
                {
                    if (param == null) continue;
                    if (param.GetType().Equals(typeof (string)))
                    {
                        genParams += string.Format(@"<param><value><string>{0}</string></value></param>", param);
                    }
                    if (param.GetType().Equals(typeof(bool)))
                    {
                        genParams += string.Format(@"<param><value><boolean>{0}</boolean></value></param>", (bool)param ? 1 : 0);
                    }
                    if (param.GetType().Equals(typeof(double)))
                    {
                        genParams += string.Format(@"<param><value><double>{0}</double></value></param>", param);
                    }
                    if (param.GetType().Equals(typeof(int)) || param.GetType().Equals(typeof(short)))
                    {
                        genParams += string.Format(@"<param><value><int>{0}</int></value></param>", param);
                    }
                    if (param.GetType().Equals(typeof (DateTime)))
                    {
                        genParams +=
                            string.Format(
                                @"<param><value><dateTime.iso8601>{0:yyyy}{0:MM}{0:dd}T{0:hh}:{0:mm}:{0:ss}</dateTime.iso8601></value></param>",
                                param);
                    }
                }
            }
            var command = 
                @"<?xml version=""1.0""?><methodCall><methodName>" + methodName + 
                @"</methodName><params>" + genParams + @"</params></methodCall>";
            var bytes = Encoding.ASCII.GetBytes(command);
            req.ContentLength = bytes.Length;
            using (var stream = req.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
            }

            using (var stream = new StreamReader(req.GetResponse().GetResponseStream()))
            {
                return stream.ReadToEnd();
            }
        }

Initial URL

                                

Initial Description
This method can execute XML-RPC call and return response. Can be useful for working with XML-RPC API's like Snipplr.com XML-RPC API and other

Initial Title
Execute XML-RPC call and return XML response as string

Initial Tags
xml, c

Initial Language
C#