/ Published in: C#
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
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/// <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; { genParams += string.Format(@"<param><value><string>{0}</string></value></param>", param); } { genParams += string.Format(@"<param><value><boolean>{0}</boolean></value></param>", (bool)param ? 1 : 0); } { genParams += string.Format(@"<param><value><double>{0}</double></value></param>", param); } { genParams += string.Format(@"<param><value><int>{0}</int></value></param>", param); } { 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); } { return stream.ReadToEnd(); } }