|
-
Oct 21st, 2006, 02:24 PM
#1
Thread Starter
New Member
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!
-
Oct 21st, 2006, 05:27 PM
#2
Fanatic Member
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
Last edited by Jumpercables; Oct 21st, 2006 at 05:31 PM.
-
Oct 21st, 2006, 07:19 PM
#3
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
-
Oct 21st, 2006, 09:38 PM
#4
Thread Starter
New Member
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!
-
Oct 21st, 2006, 09:41 PM
#5
Fanatic Member
Re: Connect to a website and download html source?
 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!
-
Oct 21st, 2006, 09:52 PM
#6
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
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
|