[VB.net] Using a WebClient

Discussion in 'Code Snippets and Tutorials' started by Andromeda, Aug 19, 2009.

  1. Andromeda

    Andromeda Level I

    Joined:
    Aug 19, 2009
    Messages:
    44
    Likes Received:
    4
    A lot of people use wrappers for grabbing website code, which is rather useless in my opinion since their main purpose is for getting and posting specific data. A webclient resembles the Inet control in VB6, except that it can only be instanciated, or declared and not placed as an object on a form. It's a member of the System.Net library, so if you do use it, you'll need to import that library or add the proper prefix when declaring it. Examples:

    Code (Text):
    1. Imports System.Net
    Code (Text):
    1. Dim Client As New System.Net.WebClient
    The webclient has several useful classes, but for now you will most likely only need the DownloadString class. It has a single property and returns a string, which will be the source code of the property (a URL) that you defined. An example would be:

    Code (Text):
    1. Dim strPage As String = Client.DownloadString("http://www.neopets.com/")
    Using the said source code of the webpage, you can proceed to use Mid functions (or GetBetween()) to find what you need, or simply InStr if your needs are less complex. This is a quick and easy solution to grab webpages and parse through them.

    I hope this helped some people out :)
     
  2. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Does the webclient handle cookie ?
    As far as I know, it's usefull only for simple task.

    edit: found what I was looking for (system.net.cookiecontainer)
     
  3. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    so are you saying that this would be a better way to make programs, with this WebClient rather than a wrapper, or are you just offering it as an alternative?
     
  4. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    I see it as an alternative.
    The wrapper was made to handle all the crap for you.
    It will store the cookies, handle them, change the headers of the connection to receive images, etc...
    I'm pretty sure you can do all this using the webclient and other system.net class, but you need to handle all this by yourself.
     
  5. Andromeda

    Andromeda Level I

    Joined:
    Aug 19, 2009
    Messages:
    44
    Likes Received:
    4
    Wrappers > webclients

    But why copy and pasta/create your own wrapper when you can do a single declaration and effectively do a simple task with as much performance? It's a little dumb to me to use a wrapper for basics, like grabbing page code...Doing something like that with wrappers only makes the size of the application bigger. So yea, an alternative for simple tasks I guess ^_^
     
  6. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Yeah, for a simple task, you can use this. But as soon as you need to login to a site, better use the wrapper.