CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    58

    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);

  2. #2
    Join Date
    Apr 2011
    Posts
    58

    Re: WebClient vs. WebRequest

    A humble and polite bump, please.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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
  •  





Click Here to Expand Forum to Full Width

Featured