执行XML-RPC调用并以字符串形式返回XML响应
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了执行XML-RPC调用并以字符串形式返回XML响应相关的知识,希望对你有一定的参考价值。
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
/// <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(); } }
以上是关于执行XML-RPC调用并以字符串形式返回XML响应的主要内容,如果未能解决你的问题,请参考以下文章