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!
Re: Connect to a website and download html source?
This code will go upto a website and get the html source code.
VB Code:
Try
Dim request As WebRequest = WebRequest.Create("http://www.vbforums.com")
Dim response As WebResponse = request.GetResponse()
Dim stream As System.IO.Stream = request.GetResponse().GetResponseStream()
Dim streamReader As System.IO.StreamReader = New System.IO.StreamReader(stream)
Dim strPageSource As String = streamReader.ReadToEnd()
End Try
Re: Connect to a website and download html source?
To display the source for this very page:
VB Code:
Using wc As New Net.WebClient
MessageBox.Show(wc.DownloadString("http://www.vbforums.com/showthread.php?t=434146"))
End Using
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!
Re: Connect to a website and download html source?
Quote:
Originally Posted by jmcilhinney
To display the source for this very page:
VB Code:
Using wc As New Net.WebClient
MessageBox.Show(wc.DownloadString("http://www.vbforums.com/showthread.php?t=434146"))
End Using
jmcilhnney you turned my 5 lines of code into one simple one nice, learned something new today!
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:
If response.StatusCode <> HttpStatusCode.Accepted Then
MessageBox.Show(response.StatusDescription, "HTTP Error " & CInt(response.StatusCode))
End If