我围绕 REST 和不太结构化的 Web 界面编写了一些非常随意的包装器,但都只是为了好玩,很少关注错误检测和处理、超时等。
有人可以给我一些关于实践或资源的指示,以便为 REST API 开发一个可靠的、专业的 .NET(或其他平台)包装器吗?
我想到的事情是:
新:从那以后我发现这几乎太基本了guide to making Yahoo REST calls , 但它作为一个起点。
重要提示:一个非常复杂且很少涉及的方面是如何处理 REST API 上的表单例份验证。单独的登录请求、cookie 存储等。
最佳答案
尽管我以 OOP 式的方式包装了无数的 Web 服务,但我发现很难为您的问题找到一个好的答案。
可以在此处找到几个 Yahoo API 示例:http://curlobjects.com/trac/browser/trunk/lib/YahooApi
1 Interpreting HTTP error codes and filtering protocol errors from API errors.
我的基础 http 类有一个可以覆盖的 http 错误回调。
2 Building URL based requests, possibly with pattern matching and token substitution.
我更喜欢字符串连接。
4 How to map an OO model to the REST model. Some requests suit static methods, e.g. getting lists, and others hang in the middle between static and instance, maybe a plain provider pattern with all static methods?
有时您需要一个大型静态工厂/提供者,有时您需要为每个方法创建一个类,有时为每组相关方法创建一个类……确实没有一种正确的方法可以解决这个问题。我总是花一些时间来感受一下服务的结构,然后尝试找到一种优雅的方式来抽象出重复的部分。
如果您对登录表单、cookie 等有任何更具体的问题,我很乐意回答。
编辑:
提到了 HttpWebRequest,我猜你要么需要设置 ClientCertificate property并正确使用 SSL,或者您可以 ignore all SLL errors带有 CertificatePolicy。
关于.net - 为 REST API 编写包装器的指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1589179/