I need to get the value of one field out of a web page.

I've never done web programming and I've never used the VB Web controls. I have no desire to learn all of the details because I doubt I'll ever have to do this again.

I have a Rogers Rocket Hub that I use at the cottage for internet access. I have been getting frequent intermittent internet problems all summer so I wrote a monitoring program which uses three threads to ping

  • The Hub (192.168.1.1)
  • The Primary DNS
  • The Secondary DNS

What I also need to do is monitor the strength of the wireless signal between the hub and the cell tower. This information is available through the web server built into the hub. The signal strength is displayed as a graphic and the name of the graphic reflects the signal strength (e.g. Strength-4.gif). The actual element that displays the image has id="Strength". I'd like to attach a screenshot but I can't get a connection for long enough to do the upload. The relevent section is:

<td align="center" id="Strength">
    <img src="http://admin/Strength-4.gif"/>

Unfortunately this code is buried about twenty levels deep in the page. I would appreciate it if anyone can tell me how to get the value in the second line programatically. The first time I go to the server I have to log in with a username/password but I don't care if I have to do this manually.

So... this code is totally untested. Hope it works for you. :)

' RocketHubReader.vb
' Compiled from sources by DeanMSands3
' Disclaimer: This code is untested. Use at your own risk.

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Net
Imports System.Xml

Class RocketHubReader
    Public Shared myUserName as String = "username"
    Public Shared myPassWord as String = "password"
    Public Shared myTargetURL As String = "http://192.168.1.1/" ' Please keep the trailing '/'
    Public Shared Sub Main()
        Dim SourceHTML As String 
        Dim DocumentXML As New XmlDocument()
        Dim StrengthTD As XmlElement
        Dim StrengthImageList As XmlNodeList
        Dim strengthImage As XmlNode
        Dim strengthImageName As String = ""
        Dim strengthImageURL As String = ""
        ' Grab the Source
        SourceHTML = GetSourceWithCredentials(myTargetURL,myUserName,myPassWord)
        ' If it worked
        If SourceHTML <>"" Then
            ' Load the xml into the XMLDocument
            DocumentXML.LoadXML(SourceHTML)
            ' Find the Element named "Strength"
            StrengthTD = DocumentXML.GetElementById("Strength")
            ' Get the Child tags named "img"
            StrengthImageList = StrengthTD.GetElementsByTagName("img")
            ' Get the first image
            strengthImage = StrengthImageList.ItemOf(0)
            ' Get URL text in "src" attribute
            strengthImageName = strengthImage.Attributes.ItemOf("src").InnerText
            ' Is this an absolute or relative address?
            If Not strengthImageName.StartsWith("http://") Then
                ' Get Base address from TargetURL
                ' Warning! This expects at least a '/' after the hostname in myTargetURL
                strengthImageURL=GetWebBaseLocation(myTargetURL)
                ' On the off chance the strengthImageName is referencing the root
                If strengthImageName.StartsWith("/") Then
                    strengthImageName = strengthImageName.Substring(1)
                End If
            End If
            ' 
            strengthImageURL = strengthImageURL + strengthImageName
            ' Download the file
            Dim Success As Boolean = DownloadFileWithCredentials(strengthImageURL, strengthImageName, myUserName, myPassWord)
            If Success Then
                Console.WriteLine("File Downloaded: " + strengthImageName)
            End If
        End If
    End Sub 'Main
    Public Shared Function GetSourceWithCredentials(ByVal target as String, ByVal userName as String, ByVal passWord as String) As String
        Dim Source as String = ""
        Try
            ' Create a request
            Dim myWebClient As WebClient = New WebClient
            ' Set credentials
            myWebClient.Credentials = New NetworkCredential(userName, passWord)
            ' Open the stream
            Dim webStream as New StreamReader(myWebClient.OpenRead(target))
            ' Read Source
            Source = webStream.ReadToEnd
            ' Return Source Code to Main
        Catch ex As HttpListenerException
            Console.WriteLine("Error accessing " + target + " - " + ex.Message)
        Catch ex As Exception
            Console.WriteLine("Error accessing " + target + " - " + ex.Message)
        End Try     
        Return Source
    End Function

    Public Shared Function DownloadFileWithCredentials(ByVal TargetURL as String, ByVal DestinationFile As String, ByVal userName as String, ByVal passWord as String) As Boolean
        Dim Success as Boolean = True
        Try
            ' Create a request
            Dim myWebClient As WebClient = New WebClient
            ' Set credentials
            myWebClient.Credentials = New NetworkCredential(userName, passWord)
            If Not (System.IO.File.Exists(DestinationFile)) Then
                myWebClient.DownloadFile(TargetURL, DestinationFile)
            End If
            ' Return Source Code to Main
        Catch ex As HttpListenerException
            Console.WriteLine("Error accessing " + TargetURL + " - " + ex.Message)
            Success=False
        Catch ex As Exception
            Console.WriteLine("Error accessing " + TargetURL + " - " + ex.Message)
            Success=False
        End Try     
        Return Success
    End Function
    Public Shared Function GetWebBaseLocation(TargetURL As String) As String
        Return TargetURL.Substring(0,TargetURL.LastIndexOf("/")+1) 
    End Function
    Public Shared Function GetWebFileName(TargetURL As String) As String
        Return TargetURL.Substring(TargetURL.LastIndexOf("/") + 1)
    End Function
End Class
' Sources: 
' http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml
' http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.getelementbyid
' http://stackoverflow.com/questions/1764815/download-a-file-which-requires-authentication-using-vb-net-c
' http://www.madarong.com/Blog/50.Using%20VB.Net%20HTTPWebRequest,%20HTTPWebResponse,%20%20WebClient%20to%20Get%20a%20Web%20Page
' http://www.techrepublic.com/blog/programming-and-development/download-files-over-the-web-with-nets-webclient-class/695
'

I ran the code and it dies at

Document.XML.LoadXml(SourceHTML)

with "The 'meta' start tag on line 3 position 2 does not match the end tag of 'head'. Line 32, position 3."

So I presume the HTML generated by the hub is not strictly compliant. The value of SourceHTML at that point is mostly commented out Javascript so I replaced it (in debug mode) with

"<html><head><meta http-equiv="Pragma" content="no-cache"><title>Rocket Hub</title></head></html>"

But I get the same error so I don't know what the problem is. But you've given me something to start with so I guess it's play time now. Thanks.

Edited 4 Years Ago by Reverend Jim

Glad to help if only a little.
Edit: This looks useful for non-strict HTML. http://htmlagilitypack.codeplex.com/
Second Edit: I just looked at my code I'd posted and realized that when I copy/pasted then modified the DownloadFileWithCredentials function, I forgot to update the comments. It should be trivial to figure out.

Edited 4 Years Ago by DeanMSands3

This article has been dead for over six months. Start a new discussion instead.