|
-
August 4th, 2011, 10:43 AM
#1
WebClient vs. WebRequest
If I need to get some data from a web site, what should I consider when choosing between WebClient and WebRequest? I have used these two methods, and they seem to do pretty much the same thing, except the WebClient does it in fewer lines of code
Uri uri = new Uri("http://myurl.com");
WebRequest wr = WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)wr.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
or
string url = "http://myurl.com";
WebClient wcl = new WebClient();
string downloadString = wcl.DownloadString(url);
-
August 5th, 2011, 12:12 PM
#2
Re: WebClient vs. WebRequest
A humble and polite bump, please.
-
August 5th, 2011, 12:19 PM
#3
Re: WebClient vs. WebRequest
Read up on the WebClient class in Msdn.
http://msdn.microsoft.com/en-us/libr...nt(VS.80).aspx
It mentions that this class internally uses the WebRequest class.
Given that, it (the WebClient) class must provide some additional functionality that the WebRequest class does not provide.
If you need this additional functionality, then use the WebClient class. If you don't, use the WebRequest class.
Just by looking at the fact that the WebClient requires way fewer lines of code to use, I believe you have already answered your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|