简体   繁体   English

如何在C#中编写REST API包装器?

[英]How to write REST API wrapper in C#?

An application I use has a REST API. 我使用的应用程序有一个REST API。 In C#, is there an example of writing a wrapper for this to invoke the API's functions? 在C#中,是否有一个为此编写包装器以调用API函数的示例?

Thanks 谢谢

Writing your own wrapper for REST usage is not terribly difficult. 为REST使用编写自己的包装器并不是非常困难。 Any client would be making use of the HttpWebRequest and HttpWebResponse objects to execute GET requests and retrieve the results. 任何客户端都将使用HttpWebRequestHttpWebResponse对象来执行GET请求并检索结果。 (There are other objects in the .Net Framework that can be used, but I've found these to be the most useful.) (.Net Framework中还有其他对象可以使用,但我发现这些对象最有用。)

This is actually the simple part. 这实际上是一个简单的部分。 How you interpret the results (read as a string, parse the xml response, deserialize to strongly-typed objects, etc.) are more involved in the decision process. 如何解释结果(读取为字符串,解析xml响应,反序列化为强类型对象等)更多地参与决策过程。 My advice to this end: keep it as simple as you can, but make your wrapper self-contained. 我对此的建议是:保持尽可能简单,但让你的包装自成一体。 Don't require your calling logic to have to know anything about sending the request or interpreting the response. 不要求您的调用逻辑必须知道有关发送请求或解释响应的任何信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM