[英]How to communicate between Python and C# using XML-RPC?
假設我有一個用 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()
函數?
不要吹我自己的號角,而是: 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,它可能對您來說不夠好,也可能不夠好。
謝謝你的回答,我從 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);
}
}
}
為了從 c# 調用 getTest 方法,您需要一個 XML-RPC 客戶端庫。 XML-RPC是此類庫的一個示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.