|
-
Jun 10th, 2004, 01:24 AM
#1
Thread Starter
Junior Member
Get HTML code from a website
Say I wanted to get the html code from www.something.com/file.html and have it shown in a textbox, what would be the best way to do this in vb.net? With VB6 i was just using inet.openurl but I don't think that's available in vb.net.
-
Jun 10th, 2004, 09:33 AM
#2
VB Code:
Dim wc As New System.Net.WebClient
Dim objStream As System.IO.Stream
Dim sr As System.IO.StreamReader
objStream = wc.OpenRead("http://www.google.com/microsoft.html")
sr = New IO.StreamReader(objStream)
Dim s As String
s = sr.ReadToEnd
MsgBox(s)
-
Jun 10th, 2004, 01:27 PM
#3
Thread Starter
Junior Member
-
Jun 10th, 2004, 05:25 PM
#4
Thread Starter
Junior Member
One problem that I found is if the website is down, the whole program will hang at the wc.OpenRead("http:/www.something.com"l) line. Is there anyway to add a timeout to it to make it stop trying to get the page after 10 secs?
-
Jun 10th, 2004, 05:30 PM
#5
Sleep mode
Originally posted by ryan0204
One problem that I found is if the website is down, the whole program will hang at the wc.OpenRead("http:/www.something.com"l) line. Is there anyway to add a timeout to it to make it stop trying to get the page after 10 secs?
That's easy . You just catch that error and it won't hang . You don't need a timer or anything . It throw an exception if anything wrong happened .
-
Jun 10th, 2004, 06:02 PM
#6
Thread Starter
Junior Member
How would I go about doing that? I tried this:
Code:
Try
objStream = wc.OpenRead(sUrl)
Catch
MsgBox(Err.Description)
End Try
and it still just hangs.
-
Jun 10th, 2004, 06:56 PM
#7
Thread Starter
Junior Member
The problem is that I need it to stop trying to get the webpage after a certain amount of seconds have passed. Without something like that it takes almost a minute before it will timeout and generate an error. I can't use a timer because while its trying to get the webpage, the whole form is frozen up.
-
Jun 10th, 2004, 10:57 PM
#8
Sleep mode
-
Jun 11th, 2004, 12:32 PM
#9
Thread Starter
Junior Member
Thanks pirate. That one works better than the first one and timesout quicker, but it doesn't seem to accept the timeout property being passed in. If I call it with a 10 second timeout, it takes about 30 seconds before it actually timesout.
-
Jun 11th, 2004, 03:28 PM
#10
why don't you use the InternetCheckConnection Api to see if the website is actually there ( connected ) , eg:
VB Code:
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Int32, ByVal dwReserved As Int32) As Int32
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strurl As String = TextBox1.Text
If InternetCheckConnection(strurl, &H1, 0) Then
Dim Request As HttpWebRequest = DirectCast(WebRequest.Create(strurl), HttpWebRequest)
Dim Response As HttpWebResponse = DirectCast(Request.GetResponse(), HttpWebResponse)
Dim istream As New IO.StreamReader(Response.GetResponseStream)
MessageBox.Show(istream.ReadToEnd)
Response.Close()
Else
MessageBox.Show("invalid url specified")
End If
End Sub
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jun 11th, 2004, 08:27 PM
#11
Thread Starter
Junior Member
good idea, i'll give that a try.
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
|