HttpWebRequest vs WebClient Option
Last post 04-23-2008 6:31 PM by liquidboy. 2 replies.
Sort Posts:
04-23-2008 3:15 PM
HttpWebRequest vs WebClient Option

Hi,

I have a query:

I have a XML file in one of the Folder in the Silverlight Hosting Web Project. I read the XML file in my Silverlight XAML file and perform my stuff.

I am using below code for reading the same:

HttpWebRequest xmlCommonData = (HttpWebRequest)WebRequest.Create(new Uri(GetAppPath() + "XML/Common.xml"));

xmlCommonData.BeginGetResponse(new AsyncCallback(CommonDataResponseCallBack), xmlCommonData);

HttpWebRequest xmlCommonData = (HttpWebRequest)asyncResult.AsyncState;

HttpWebResponse CommonDataResponse = (HttpWebResponse)xmlCommonData.EndGetResponse(asyncResult);

Stream CommonDataStream = CommonDataResponse.GetResponseStream();

using (XmlReader reader = XmlReader.Create(CommonDataStream))

.......My custom code....

I want to know If this approach of reading the XML file is better or using the WebClient approach..Can someone tell me the difference betwen two?

WebClient code:

WebClient webClient = new WebClient();

webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);

webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);

try

{

webClient.DownloadStringAsync(
new Uri("http://localhost:8888/xdata.xml"));

}

....

regards,

Parimal

parimaln

Loading...
Joined on 01-24-2008
Posts 100
04-23-2008 4:47 PM
Marked as Answer
Re: HttpWebRequest vs WebClient Option

If WebClient works, I would use it instead of HttpWebRequest since it's simpler!

From Tim Heuer's blog - http://timheuer.com/blog/archive/2008/03/14/calling-web-services-with-silverlight-2.aspx

Instead in Silverlight you'll want to use WebClient or HttpWebRequest.  What's the difference?  Here's the timheuer version.  WebClient is a simpler implementation doing GET requests really easily and get a response stream.  HttpWebRequest is great for when you need a bit more granular control over the request, need to send headers or other customizations.

Hope this helps!

Wilfred Pinto
http://projectsilverlight.blogspot.com
 

Wilfred Pinto

Loading...
Joined on 04-03-2008
Los Angeles, California, USA
Posts 143
04-23-2008 6:31 PM
Marked as Answer
Re: HttpWebRequest vs WebClient Option

I tend to use webclient for simple GET requests

The more powerful HTTP api's for POST requests and setting headers

Great article that explains the design decisions behind the networking stack by one of the Silverlight PMs intimate with those bits (KAREN CORBY)

http://scorbs.com/2008/04/05/silverlight-http-networking-stack-part-1-site-of-origin-communication/

Another great link is from WILCO BAUWER also from the Silverlight Team and charged with the Networking Bits too. He wrote a great post and some help classes to make networking calls simpler (also allowing you to use sync calls and SL 1.1 style networking calls via the browser XMLHTTPRequest object)

http://www.wilcob.com/Wilco/News/http-requests-in-silverlight.aspx

I use Wilco's libraries because they are really simple to understand, I suggest taking a look at them

 

Learn SilverLight with me!

liquidboy

Loading...
Joined on 08-07-2006
Sydney
Posts 54
Page view counter