This is the "GetWeb" class:
Imports System.IO
Imports System.Net
Imports System.Text
Public Class Base
Implements IDisposable
' Implement IDisposable.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
' Free other state (managed objects).
End If
' Free your own state (unmanaged objects).
' Set large fields to null.
End Sub
Protected Overrides Sub Finalize()
' Simply call Dispose(False).
Dispose(False)
End Sub
End Class
Public Class getweb
Inherits Base
Public session As WebSession
Public responseContent As String
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
' Release managed resources.
End If
' Release unmanaged resources.
' Set large fields to null.
' Call Dispose on your base class.
MyBase.Dispose(disposing)
End Sub
Public Function HTTPGet(ByVal strURL As String) As Boolean
Try
Dim webRequest As System.Net.HttpWebRequest
Dim webResponse As System.Net.HttpWebResponse
If session Is Nothing Then
session = New WebSession
End If
System.Net.ServicePointManager.CertificatePolicy = New MyPolicy
session.LastResponse = String.Empty
webRequest = CType(System.Net.WebRequest.Create(strURL), System.Net.HttpWebRequest)
webRequest.AllowAutoRedirect = True
session.Headers = New System.Net.WebHeaderCollection
webRequest.Headers.Add(session.Headers)
webRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
webRequest.KeepAlive = True
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)"
webRequest.Timeout = 90000
webRequest.Method = "GET"
webRequest.Headers.Add("GET", webRequest.RequestUri.PathAndQuery & " HTTP/1.1")
webRequest.Referer = String.Empty
webRequest.CookieContainer = New CookieContainer
If Not IsNothing(session.Cookies) Then
webRequest.CookieContainer.Add(session.Cookies)
Else
webRequest.CookieContainer.Add(New CookieCollection)
End If
webResponse = CType(webRequest.GetResponse(), System.Net.HttpWebResponse)
Dim responseStream As System.IO.Stream = webResponse.GetResponseStream()
Dim responseEncoding As System.Text.Encoding = New System.Text.UTF8Encoding
' Pipes the response stream to a higher level stream reader with the required encoding format.
Dim responseReader As New StreamReader(responseStream, responseEncoding)
responseContent = responseReader.ReadToEnd()
'Dim responseContent As String = responseReader.ReadToEnd()
session.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri)
webRequest = Nothing
webResponse = Nothing
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Class MyPolicy
Implements ICertificatePolicy
Public Function CheckValidationResult(ByVal srvPoint As System.Net.ServicePoint, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal request As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean Implements System.Net.ICertificatePolicy.CheckValidationResult
' i really could care less if the cert is dead and buried, accept it because it is a must
Return True
End Function
End Class
End Class
And the code we use to call the class:
private sub dowork()
Dim webreq As New getweb
If webreq.HTTPGet("
False) Then
Dim content As String = webreq.responseContent
If content.IndexOf("TheIP: ") >= 0 Then
'Find where the IP address starts
Dim ipaddress As String = content.Substring(content.IndexOf("TheIP: ") + 7, Len(content) - content.IndexOf("TheIP: ") - 7)
'Trim the right bit of the resulting HTML off so all we have is the IP address
ipaddress = ipaddress.Substring(0, ipaddress.IndexOf("<BR>"))
'Drop new IP addresses into a text box
If TextBox1.Text.IndexOf(ipaddress) < 0 Then
TextBox1.Text &= ipaddress & vbCrLf
End If
End If
End If
webreq.Dispose()
webreq = nothing
end sub
ASP, VB, VBS, Cold Fusion, SQL, DTS, T-SQL, PL-SQL, IBM-MQ, Crystal Reports, Crystal Enterprise