且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中调用基于XML-RPC规范的API?

更新时间:2023-02-11 13:37:26

第1步:在 .NET 中创建了控制台应用程序

第2步:安装 NuGet xml-rpc.net

第3步:创建示例 request 模型类像这样

Step 3: Created a sample request model class like this,

 public class request
    {
        public string username { get; set; }
        public string password { get; set; }
    }

第4步:创建示例响应模型

public class response
    {
        public int id { get; set; }
        public int status { get; set; }        
    }

第5步:创建一个界面命名空间 的帮助下,使用CookComputing.XmlRpc继承了 IXmlRpcProxy 基类

,并且此接口必须包含我们的终结点方法,并且应使用具有 API 的过滤器 XmlRpcUrl 装饰/ strong>资源。

Step 5 : Create an interface which is inherited form IXmlRpcProxy base class with the help of the namespace using CookComputing.XmlRpc; and this interface must contain our endpoint method and it should decorate with the filter XmlRpcUrl having the API resource.

    [XmlRpcUrl("https://api.XXX.com/XXX")]
    public interface FlRPC : IXmlRpcProxy
    {
        [XmlRpcMethod("login")]//endpoint name
        response login(request request);
    }

第6步:调用 XML-RPC服务器,有必要使用 proxy 类的实例。

Step 6 : To make calls to an XML-RPC server it is necessary to use an instance of a proxy class.

class Program
    {
        static void Main(string[] args)
        {
            response response = new response();
            request request = new request();
            FlRPC proxy = XmlRpcProxyGen.Create<FlRPC>();
            request.password = "xxxxxxxx";
            request.username = "xxxx@xxxx.org";
            response = proxy.login(request);
        }
    }

注意: 请求响应模型类必须包含所有属性,并且属性名称应比端点的请求,响应的有效负载更苗条。

Note: The above request, response model class must contain all the properties and the property name should be slimier to the payload of the endpoint's request, response.