Results 1 to 6 of 6

Thread: Connect to a website and download html source?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    5

    Angry Connect to a website and download html source?

    Hi!

    I just went from vb 6.0 to VB.net. And I would like to use the VB.net way for connecting to a website. But I don't know how??

    What I would like is:
    * connect to a site
    * get the html
    * get the response code (like 200 is online, 404 = not excist)

    I did this with iNet before, but I don't know how to do this in Vb. I've seen similar questions on forums, but I just can't make 'em work.

    Please help!

    Thanks!

  2. #2
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Connect to a website and download html source?

    This code will go upto a website and get the html source code.

    VB Code:
    1. Try
    2.    Dim request As WebRequest =  WebRequest.Create("http://www.vbforums.com")
    3.    Dim response As WebResponse =  request.GetResponse()
    4.    Dim stream As System.IO.Stream =  request.GetResponse().GetResponseStream()
    5.    Dim streamReader As System.IO.StreamReader =  New System.IO.StreamReader(stream)
    6.    Dim strPageSource As String = streamReader.ReadToEnd()
    7. End Try
    Last edited by Jumpercables; Oct 21st, 2006 at 05:31 PM.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect to a website and download html source?

    To display the source for this very page:
    VB Code:
    1. Using wc As New Net.WebClient
    2.     MessageBox.Show(wc.DownloadString("http://www.vbforums.com/showthread.php?t=434146"))
    3. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    5

    Re: Connect to a website and download html source?

    Hi!

    Thank you guys for those greate responses.
    Do you know how to get the status code? Like response 200, 403, 404 etc?

    Thanks!

  5. #5
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: Connect to a website and download html source?

    Quote Originally Posted by jmcilhinney
    To display the source for this very page:
    VB Code:
    1. Using wc As New Net.WebClient
    2.     MessageBox.Show(wc.DownloadString("http://www.vbforums.com/showthread.php?t=434146"))
    3. End Using
    jmcilhnney you turned my 5 lines of code into one simple one nice, learned something new today!

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Connect to a website and download html source?

    If you use Jumpercables' code but with HttpWebRequest and HttpWebResponse objects specifically you can then get the StatusCode property of the response. Check out the help topic for the HttpStatusCode enumeration for all the status codes and the enumerated values that they correspond to. Also take note of the response's StatusDescription property. You might use them something like this:
    VB Code:
    1. If response.StatusCode <> HttpStatusCode.Accepted Then
    2.     MessageBox.Show(response.StatusDescription, "HTTP Error " & CInt(response.StatusCode))
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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