访问 XML RPC 接口

本文关键字:接口 RPC XML 访问 | 更新日期: 2023-09-27 18:30:55

我有一些带有rtorrent的服务器。我想获取有关这些服务器的状态信息并删除种子,因为我想使用 rtorrent 的 XML rpc 接口。

scgi_port = 本地主机:5000https://github.com/rakshasa/rtorrent/wiki/RPC-Setup-XMLRPC

现在我需要一些帮助才能将界面中的信息带回我的程序。我已经有一些代码,但是执行时总是出错。

其它信息:无法建立与远程服务器的连接。

using System;
using System.Text;
using System.Net;
using System.IO;
namespace SimpleXmlRpcClient
{
    class Program
    {
        static void Main(string[] args)
        {
            WebRequest request = WebRequest.Create("http://ip/RPC2");
            request.Method = "POST";
            string postData = @"<?xml version=""1.0""?>
            <methodCall>
              <methodName>system.listMethods
            </methodCall>";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = request.GetResponse();
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Console.WriteLine(responseFromServer);
            reader.Close();
            dataStream.Close();
            response.Close();
            Console.WriteLine("Press any key to continue ...");
            Console.ReadKey();
        }
    }
}

访问 XML RPC 接口

我终于找到了解决方案。

您仍然解析了 XML 编码的响应,但现在我终于得到了响应。

System.Net.ServicePointManager.Expect100Continue = false;
                byte[] requestData = Encoding.ASCII.GetBytes("<?xml version='"1.0'"?><methodCall><methodName>system.listMethods</methodName></methodCall>");
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ip/RPC2");
                request.Method = "POST";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;)";
                request.ContentType = "text/xml";
                request.ContentLength = requestData.Length;
                using (Stream requestStream = request.GetRequestStream())
                    requestStream.Write(requestData, 0, requestData.Length);
                string result = null;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
                            result = reader.ReadToEnd();
                    }
                }

很好!,Olias 的解决方案是正确的。