如何通过XML-RPC在Python和C#之间通信?

7 投票
3 回答
3511 浏览
提问于 2025-04-15 12:43

假设我有一个用Python实现的简单XML-RPC服务:

from SimpleXMLRPCServer import SimpleXMLRPCServer  # Python 2

def getTest():
    return 'test message'

if __name__ == '__main__' :
    server = SimpleXMLRPCServer(('localhost', 8888))
    server.register_function(getTest)
    server.serve_forever()

有人能告诉我怎么从C#调用这个getTest()函数吗?

3 个回答

1

要在C#中调用getTest方法,你需要一个XML-RPC客户端库。XML-RPC就是这样一个库的例子。

3

谢谢你的回答,我试了darin链接中的xml-rpc库。我可以用以下代码调用getTest函数。

using CookComputing.XmlRpc;
...

    namespace Hello
    {
        /* proxy interface */
        [XmlRpcUrl("http://localhost:8888")]
        public interface IStateName : IXmlRpcProxy
        {
            [XmlRpcMethod("getTest")]
            string getTest();
        }

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                /* implement section */
                IStateName proxy = (IStateName)XmlRpcProxyGen.Create(typeof(IStateName));
                string message = proxy.getTest();
                MessageBox.Show(message);
            }
        }
    }
3

我不是想自夸,但你可以看看这个链接:http://liboxide.svn.sourceforge.net/viewvc/liboxide/trunk/Oxide.Net/Rpc/

class XmlRpcTest : XmlRpcClient
{
    private static Uri remoteHost = new Uri("http://localhost:8888/");

    [RpcCall]
    public string GetTest()
    {
        return (string)DoRequest(remoteHost, 
            CreateRequest("getTest", null));
    }
}

static class Program
{
    static void Main(string[] args)
    {
        XmlRpcTest test = new XmlRpcTest();
        Console.WriteLine(test.GetTest());
    }
}

这个应该能解决你的问题……需要注意的是,上面的库是LGPL许可证,这可能对你来说足够好,也可能不够。

撰写回答