2016-08-22 24 views
0

如何编写一个负责连接到restful api的库? lib应该提供直接访问api函数的方法。正在使用WebClient以字符串形式获得响应,并且将其作为对象进行反序列化成为可靠的可接受的方式来执行此操作?C#在生产中使用Json WebService

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");  
} 

回答

1

您可以为每组呼叫(用户管理,上传/下载数据...)制作一个类。这个类实现了一些功能(映射网络调用)。所以当你要列出所有新成员,那么你可以写

var accounting = new Accounting(); 
var memberList = accounting.GetNewMember(); 

此功能(GetNewMember())是一个简单的包装。在这个函数中,您可以调用REST-full API,然后将JSON转换为您想要的结果。

而对于创建URIs,您可以使用string.Format(string,object [])重载。

1

您可以这样做,或者您可以使用Visual Studio和Swagger的代码生成功能在您的解决方案中导入剩余Web API。

+0

谢谢,我已经使用swagger,我不知道我可以用它来自动导入!对不起,我已经写了整个Api连接器。 :d – appl3r