从 .NET 使用 REST API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1842770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consume RESt API from .NET
提问by Ajish
I am trying to consume REST API from my .NET Application. This API's are all written in JAVA. I am asked to pass the authentication credentials vis HTTP headers. How can I pass these authentication credentials like 'DATE', 'AUTHORIZATION' and 'Accept' via HTTP headers.
我正在尝试从我的 .NET 应用程序中使用 REST API。这个API都是用JAVA写的。我被要求通过 HTTP 标头传递身份验证凭据。如何通过 HTTP 标头传递这些身份验证凭据,如“日期”、“授权”和“接受”。
Which class in .NET can I use to accomplish this task. Can anyone help me with this?
我可以使用 .NET 中的哪个类来完成此任务。谁能帮我这个?
All your help will be appreciated.
您的所有帮助将不胜感激。
Ajish.
阿吉什。
回答by Darrel Miller
Update
更新
This library has now been replaced by http://nuget.org/packages/Microsoft.Net.Http/2.1.10
该库现已被http://nuget.org/packages/Microsoft.Net.Http/2.1.10取代
Use the Microsoft.Http client library that is in WCF REST Starter Kit Preview 2.
使用WCF REST Starter Kit Preview 2 中的 Microsoft.Http 客户端库。
Here is how you could use it:
以下是您可以如何使用它:
var client = new HttpClient();
client.DefaultHeaders.Authorization = new Credential("ArbitraryAuthHeader");
client.DefaultHeaders.Date = DateTime.Now;
client.DefaultHeaders.Accept.Add("application/xml");
var response = client.Get("http://example.org");
var xmlString = response.Content.ReadAsString();
回答by autonomatt
Just to add a bit of value to this thread (I too was looking for a way to consume a RESTful service and easily provide credentials and came across this thread ... I did not have the "Date" requirement), Aaron Skonnard has written an excellent article on using the WCF REST Starter Kit called:
只是为了给这个线程增加一点价值(我也在寻找一种方法来使用 RESTful 服务并轻松提供凭据并遇到了这个线程......我没有“日期”要求),Aaron Skonnard 写道一篇关于使用 WCF REST Starter Kit 的优秀文章称为:
A Developer's Guide to the WCF REST Starter Kit
There is a very informative section on how to consume a RESTful service using HttpClient. And here's the code snippet to talk to Twitter:
关于如何使用 HttpClient 使用 RESTful 服务,有一个非常有用的部分。这是与 Twitter 对话的代码片段:
HttpClient http = new HttpClient("http://twitter.com/statuses/");
http.TransportSettings.Credentials =
new NetworkCredential("{username}", "{password}");
HttpResponseMessage resp = http.Get("friends_timeline.xml");
resp.EnsureStatusIsSuccessful();
ProcessStatuses(resp.Content.ReadAsStream());
回答by Mitchel Sellers
回答by Mark Seemann
Despite its somewhat misleading name, ADO.NET Data Services(which is part of .NET 3.5) contains APIs for both exposing and consuming REST-based services. In your case you can safely ignore the part that allows you to expose services and concentrate on the client part.
尽管名称有些误导,但ADO.NET 数据服务(它是 .NET 3.5 的一部分)包含用于公开和使用基于 REST 的服务的 API。在您的情况下,您可以安全地忽略允许您公开服务并专注于客户端部分的部分。
It supports LINQ and all sorts of goodness, allowing you to query your REST service like this:
它支持 LINQ 和各种优点,允许您像这样查询 REST 服务:
var selectedOrders = from o in context.Orders
where o.Freight > 30
orderby o.ShippedDate descending
select o;
There's more about it here. Give it a try - I've been pretty happy with it so far.
这里有更多关于它的信息。试一试 - 到目前为止,我对它非常满意。

