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.
Printable View
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.
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)
thanks for the code
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 .Quote:
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?
How would I go about doing that? I tried this:
and it still just hangs.Code:Try
objStream = wc.OpenRead(sUrl)
Catch
MsgBox(Err.Description)
End Try
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.
See if this helps !!
http://www.developer.com/net/vb/article.php/3113371
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.
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
good idea, i'll give that a try.