Search code examples
c#xmlrpcxml-rpc

Accessing XML RPC Interface


i have some servers with rtorrent. I want to get status information about these servers and delete torrents for that i want to use the XML rpc interface of rtorrent.

scgi_port = localhost:5000 https://github.com/rakshasa/rtorrent/wiki/RPC-Setup-XMLRPC

Now i rly need some help to get the information from the interface back to my programm. i already have some code, but i always get an error when executing.

Additional information: The Connection with the remote server could not be established.

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();
        }
    }
}

Solution

  • i finally found the solution.

    you still have parse the xml encoded response but now i finally get an response.

    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();
                        }
                    }