从WCF使用REST服务

时间:2023-02-08 09:49:53

I'm not that familiar with WCF, but I thought I'll learn while trying to consume an existing service.

我对WCF并不熟悉,但我想我会在尝试使用现有服务时学习。

One of the REST APIs I thought of was the Twitter API. I thought of developing a WPF client that will just output to the screen the last 5 tweets by a certain Twitter user.

我想到的一个REST API是Twitter API。我想开发一个WPF客户端,它只会在某个Twitter用户的最后5条推文中输出到屏幕。

I was wondering if someone could please briefly outline the steps I need to take in Visual Studio to consume these services, using WCF (classes, wizards, proxies etc.).I already know how to just call them using a web request and parse the XML that returns - I really want to see the WCF part at work.

我想知道是否有人可以请使用WCF(类,向导,代理等)简要概述我需要在Visual Studio中使用这些服务的步骤。我已经知道如何使用Web请求调用它们并解析返回的XML - 我真的希望看到WCF部分正常工作。

Thanks in advance to anyoine who helps further my education :)

在此先感谢帮助我继续教育的anyoine :)

2 个解决方案

#1


7  

Check out Kirk Evans Creating a REST Twitter Client With WCF. The latest improvements to WCF in .NET 3.5 SP1 make many RESTful interfaces easier.

查看Kirk Evans使用WCF创建REST Twitter客户端。 .NET 3.5 SP1中WCF的最新改进使许多RESTful接口更容易。

Also check out the Twitter WCF 3.5 API Declaration Library from the MSDN site.

另请查看MSDN站点上的Twitter WCF 3.5 API声明库。

Here's yet another example - WARNING as of 2/3/10 link is "borked"

这是另一个例子 - 警告截至2/3/10链接是“borked”

#2


6  

There is no benefit to using WCF to consume an Http based API like the Twitter API. System.Net.HttpWebRequest is more than sufficient. In fact I suspect that you will have some difficulty. WCF is much easier to get working when you have WCF at both ends of the wire.

使用WCF来使用基于Http的API(如Twitter API)没有任何好处。 System.Net.HttpWebRequest绰绰有余。事实上,我怀疑你会遇到一些困难。当您在线路的两端都有WCF时,WCF更容易上班。

However, if the REST API is returning Atom content then you could using the System.ServiceModel.Syndication classes to help parse the response.

但是,如果REST API返回Atom内容,那么您可以使用System.ServiceModel.Syndication类来帮助解析响应。

EDIT: Since I wrote this post Microsoft released a preview of a new HTTP client library that does an even better job of consuming RESTful services.
Here is how you would use it to POST to twitter:

编辑:自从我写这篇文章以来,微软发布了一个新的HTTP客户端库的预览版,它可以更好地使用RESTful服务。以下是如何使用它来发布到Twitter:

var client = new HttpClient();
client.DefaultHeaders.Authorization = Credential.CreateBasic("username","password");
var form = new HttpUrlEncodedForm();
form.Add("status","Test tweet using Microsoft.Http.HttpClient");
var content = HttpContent.Create(form);
var resp = client.Post("http://www.twitter.com/statuses/update.xml", content);

If you want more more details on this client library, I am in the process of writing some blog posts about it here.

如果您想了解有关此客户端库的更多详细信息,我现在正在撰写一些关于它的博客文章。

#1


7  

Check out Kirk Evans Creating a REST Twitter Client With WCF. The latest improvements to WCF in .NET 3.5 SP1 make many RESTful interfaces easier.

查看Kirk Evans使用WCF创建REST Twitter客户端。 .NET 3.5 SP1中WCF的最新改进使许多RESTful接口更容易。

Also check out the Twitter WCF 3.5 API Declaration Library from the MSDN site.

另请查看MSDN站点上的Twitter WCF 3.5 API声明库。

Here's yet another example - WARNING as of 2/3/10 link is "borked"

这是另一个例子 - 警告截至2/3/10链接是“borked”

#2


6  

There is no benefit to using WCF to consume an Http based API like the Twitter API. System.Net.HttpWebRequest is more than sufficient. In fact I suspect that you will have some difficulty. WCF is much easier to get working when you have WCF at both ends of the wire.

使用WCF来使用基于Http的API(如Twitter API)没有任何好处。 System.Net.HttpWebRequest绰绰有余。事实上,我怀疑你会遇到一些困难。当您在线路的两端都有WCF时,WCF更容易上班。

However, if the REST API is returning Atom content then you could using the System.ServiceModel.Syndication classes to help parse the response.

但是,如果REST API返回Atom内容,那么您可以使用System.ServiceModel.Syndication类来帮助解析响应。

EDIT: Since I wrote this post Microsoft released a preview of a new HTTP client library that does an even better job of consuming RESTful services.
Here is how you would use it to POST to twitter:

编辑:自从我写这篇文章以来,微软发布了一个新的HTTP客户端库的预览版,它可以更好地使用RESTful服务。以下是如何使用它来发布到Twitter:

var client = new HttpClient();
client.DefaultHeaders.Authorization = Credential.CreateBasic("username","password");
var form = new HttpUrlEncodedForm();
form.Add("status","Test tweet using Microsoft.Http.HttpClient");
var content = HttpContent.Create(form);
var resp = client.Post("http://www.twitter.com/statuses/update.xml", content);

If you want more more details on this client library, I am in the process of writing some blog posts about it here.

如果您想了解有关此客户端库的更多详细信息,我现在正在撰写一些关于它的博客文章。