Getting IP and IMG via RegEX and HttpWebRequest

Jun 26 2012 5:37 PM
Hi,

I am using httpwebrequest and regex in order to open a website and get some of its information out of it... I coded something that worked a few weeks ago but now it just won't work anymore antlough i don't see any credible changes in the websites sourcecode that could lead to that.

my code that used to work:

    Private Sub ipcheckBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ipcheckBTN.Click
If pubipRTF.Text = "" Then
MsgBox("Please enter an IP-Adress...")
Else
Try
Dim httpRequest As HttpWebRequest = HttpWebRequest.Create("http://whatismyipaddress.com/ip/" & pubipRTF.Text)
Dim httpResponse As HttpWebResponse = httpRequest.GetResponse()
Dim reader As StreamReader = New StreamReader(httpResponse.GetResponseStream)
Dim httpContent As String = reader.ReadToEnd
ipcheckBTN.Text = "..."
Dim re1 As String = ".*?" 'Non-greedy match on filler
Dim re2 As String = "(http:\/\/whatismyipaddress\.com\/images\/flags\/(?:[a-z][a-z0-9_]*)\.png)" 'flag
Dim re3 As String = "(</th><td>(?<country>([\sa-zA-Z0-9.:-_/]+)) <img )" 'country

Dim r1 As Regex = New Regex(re1 + re2, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
Dim m1 As Match = r1.Match(httpContent)
If (m1.Success) Then
Dim httpurl1 = m1.Groups(1)
flagPICT.ImageLocation = httpurl1.ToString
flagurlRTF.Text = httpurl1.ToString
End If
For Each m As Match In Regex.Matches(httpContent, re3, RegexOptions.Singleline Or RegexOptions.IgnoreCase)
locationRTF.Text = m.Groups("country").ToString
Next
ipcheckBTN.Text = "Check"
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub


Right now, whenever I try to execute that comand it parses an error line that the website doesn't respond.

Without exceptionhandler it shows me that

Dim httpRequest As HttpWebRequest = HttpWebRequest.Create("http://whatismyipaddress.com/ip/" & pubipRTF.Text)
causes this error.

Do you have any tips that can lead to the possible fix?