Results 1 to 11 of 11

Thread: Get HTML code from a website

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19

    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.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    VB Code:
    1. Dim wc As New System.Net.WebClient
    2.         Dim objStream As System.IO.Stream
    3.         Dim sr As System.IO.StreamReader
    4.         objStream = wc.OpenRead("http://www.google.com/microsoft.html")
    5.         sr = New IO.StreamReader(objStream)
    6.         Dim s As String
    7.         s = sr.ReadToEnd
    8.         MsgBox(s)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    thanks for the code

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    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?

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    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.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    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.

  10. #10
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    why don't you use the InternetCheckConnection Api to see if the website is actually there ( connected ) , eg:
    VB Code:
    1. Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Int32, ByVal dwReserved As Int32) As Int32
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim strurl As String = TextBox1.Text
    5.             If InternetCheckConnection(strurl, &H1, 0) Then
    6.                 Dim Request As HttpWebRequest = DirectCast(WebRequest.Create(strurl), HttpWebRequest)
    7.                 Dim Response As HttpWebResponse = DirectCast(Request.GetResponse(), HttpWebResponse)
    8.                 Dim istream As New IO.StreamReader(Response.GetResponseStream)
    9.                 MessageBox.Show(istream.ReadToEnd)
    10.                 Response.Close()
    11.             Else
    12.                 MessageBox.Show("invalid url specified")
    13.             End If
    14.     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]

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    19
    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
  •  



Click Here to Expand Forum to Full Width