[英]C# Using a Json WebService in production
一个人应该如何编写一个负责连接静态api的库? lib应该提供直接访问api函数的方法。 使用WebClient
以字符串形式获取响应,并使用Json.NET
将其反序列化为对象是一种可靠的,可接受的方法吗?
ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true;//the server uses a self signed SSL cert
using (WebClient client = new WebClient())
{
client.Headers.Add("..", "....");
string str = client.DownloadString("https://..../users/"+UserId+"/super-secret-data");
}
您可以为每组通话(用户管理,上传/下载数据等)创建一个类。 此类实现一些功能(映射Web调用)。 因此,当您要列出所有新成员时,您可以编写
var accounting = new Accounting();
var memberList = accounting.GetNewMember();
这个函数(GetNewMember())只是一个包装器。 在该函数中,您调用REST-full API,将JSON返回,然后将其转换为所需的结果。
对于创建URI,可以使用string.Format(string,object [])重载。
您可以这样做,也可以将代码生成与Visual Studio和swagger结合使用,以在解决方案中导入其余的Web api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.