Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to read  HTML Page with  vb.NET

Posted on 2006-05-09
4
Medium Priority
?
5,033 Views
Last Modified: 2010-09-15
Hi all   ,  i  am using VB.NET   and i want my app to browse to
"Whatismyip.com"   And extract the IP Address from the Body or from the Tittle.

Does anyone have any example of how should i do it ???  
0
Comment
Question by:JudaB
4 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16637318
Hello JudaB,

this is a sample to show you ip address on a page, it will download the page and show in a messagebox you can parse the strRequestedHTML yourself in vb
----------
Imports System.Net
Imports System.Text

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objWebClient As New WebClient()

        'STEP 2: Call the DownloadedData method
        Const strURL As String = "http://www.ipchicken.com/"
        Dim aRequestedHTML() As Byte

        aRequestedHTML = objWebClient.DownloadData(strURL)

        'STEP 3: Convert the Byte array into a String
        Dim objUTF8 As New UTF8Encoding()
        Dim strRequestedHTML As String
        strRequestedHTML = objUTF8.GetString(aRequestedHTML)

        'display the string
        MessageBox.Show(strRequestedHTML)

    End Sub
End Class
----------

hope this helps a bit
bruintje
0
 
LVL 2

Accepted Solution

by:
barisgomlek earned 200 total points
ID: 16637586
This is exaclty what you are looking for, I implement it for your needs, I tested it, it is working :)

Use this in your code like :
IpService.GetIP()

You do not need to create an instance of the class

Public Class IPService
    Private Const IP_SERVICE_URL = "http://www.whatismyip.com"
    Private Const SEARCH_STRING_BEGIN = "<h1>Your IP Is "
    Private Const SEARCH_STRING_END = "</h1>"

    Public Shared Function GetIP() As String

        Dim web As New WebClient
        Dim html() As Byte
        Dim resultHtml As String

        Dim utf8 As New UTF8Encoding
        html = web.DownloadData(IP_SERVICE_URL)
        resultHtml = utf8.GetString(html)

        'Find IP
        Dim startIndex As Integer = resultHtml.IndexOf(SEARCH_STRING_BEGIN, 0)
        Dim endIndex As Integer = resultHtml.IndexOf(SEARCH_STRING_END, startIndex)
        Dim lenght As Integer = endIndex - startIndex - SEARCH_STRING_BEGIN.Length

        Return resultHtml.Substring(startIndex + SEARCH_STRING_BEGIN.Length, lenght)

    End Function
End Class
0
 
LVL 3

Expert Comment

by:pitster
ID: 16639433
If you are just trying to get the IP of the client you can do this:
Dim clientIP as String
clientIP = Request.ServerVariables("REMOTE_ADDR").ToString

This will return a clients IP Address




I hope this helps :)
0
 

Expert Comment

by:Rusty_Shackelford
ID: 33688385
everything up til this comment is wrong...
i will use the existing code to show the correct way

you cant call the page http://www.whatismyip.com since they scramble the URL...you have to call the automation link at http://www.whatismyip.com/automation/n09230945.asp

when calling the automation link, as shown in the attached code, resultHtml is the client IP as a workable string....enjoy and remember, if you are using a service like www.whatismyip.com for programs, they probably have an automation page for you to call instead of using the main customer facing site.
Imports System.Net
Imports System.Text
Public Class IPService
    Private Const IP_SERVICE_URL = "http://www.whatismyip.com/automation/n09230945.asp"
    
    Public Shared Function GetIP() As String

        Dim web As New WebClient
        Dim html() As Byte
        Dim resultHtml As String

        Dim utf8 As New UTF8Encoding
        html = web.DownloadData(IP_SERVICE_URL)

        resultHtml = utf8.GetString(html)
        GetIp = resultHtml
        Return
    End Function

End Class

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question