Silverlight用WebClient and HttpWebRequest两种方式来调用WebService

本文通过实例演示了如何使用WebClient和HttpWebRequest从WebService获取数据。这两种方法均可用于检索数据,但WebClient更适合一次性数据获取且使用更为简便。而HttpWebRequest则提供了更多的控制选项,如设置头部信息等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天面试了一家公司,是关于Silverlight的。上来就问我,WebClient 与HttpWebRequest调用WebService的区别,一问我我就蒙了。因为平时做项目的话,对这两种概念不用太了解也可以开发。就是用VS.NET自带的一个功能,参照WebService就可以自动接口类,帮我们都做好了。

具体图解请参照:http://blog.youkuaiyun.com/ourmessage/archive/2008/03/27/2223497.aspx

 

微软帮我们做好了以后,容易让我们这些开发者在不知道怎么回事的情况下就可以开发,但这也许会害了我们。比如说我的面试。

然后我就在老外的网站上找到了如何直接调用WebService的例子,为了跟大家分享,也为了自己以后参照用,所以就转一下他的帖子。我也不翻译了。大家自己看吧。英语很简单。以下为转载内容。


The WebClient and HttpWebRequest classes can both be used for retrieving data from a Web service. WebClient is most useful for one-time retrieval of data from a Web service, or downloading remote resources (for more information on this see, "Downloading Content on Demand" ) and I think, is somewhat easier to use. HttpWebRequest is perhaps a bit less user-friendly, but will enable you to set headers and generally offers more control over a Web service request.

 

The following code demonstrates how to make the same request using both classes. I’ve set up a simple Silverlight control that contains two buttons, a text box to capture user input, and a text block to display the content returned from the Web service. The first example demonstrates how to make a request using WebClient , the second demonstrates the same request using HttpWebRequest . Both requests are asynchronous.

 

First, I show you the simple UI. It’s some stack panels, two buttons, a text box that accepts user and a text block to display the output from the Web service.

The request will go to a Digg Web service that returns the last photos uploaded to its gallery service. This service that allows you to use the URL to specify the expected response type and the number of photos to return, which I will do. In addition, I must specify an AppKey, which is required by the Digg Web service. It’s important to notice that Digg has the required cross domain policy in place to allow calls from my Silverlight application.

 

First I declare the base URL used by both WebClient and HttpWebRequest :

string baseUri = http://services.digg.com/galleryphotos?count ;

 

 

I’ll use the same URL with both WebClient and HttpWebRequest , but since I'll append the number of photos to return, I’ll build the complete URL for each request using the specified number and the AppKey. In addition, I'll specify an xml response by appending &type=xml to the end of the URL.

 

To call the service by using WebClient , declare a WebClient object, and in the Page constructor associate its DownloadStringCompleted event with a handler.

Next, I’ll made the same call using HttpWebRequest . In Button2_Click , I create the HttpWebRequest by calling HttpWebRequest.Create , passing the URL built from the baseUr l, the user-specified photo count, and AppKey. I call BeginGetResponse on the request, passing the callback method as a new AsyncCallback object and the request object as the state parameter. I’ll use the request object in the callback to retrieve the response.

In the callback method, ReadCallback , I retrieve the request with the AsyncState property of the asynchronousResult parameter, and get the response by calling EndGetResponse on the request. Finally I use a StreamReader to read the results and output them to the TextBlock.

The results are essentially the same either way, but for a more complex request, if I need to specify headers, content type or need more control over my request, I’ll need to use HttpWebRequest .

 

Hopefully this helps you get started with your own Web service calls. If you need more help, see my previous post , which also points to two other great blog posts on the topic.


原文地址:http://blogs.msdn.com/b/silverlight_sdk/archive/2008/04/01/using-webclient-and-httpwebrequest.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值