Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simulate new Browser using WebRequest

Status
Not open for further replies.

Wholsea

Programmer
Jun 16, 2004
138
US
Scenerio:

We have 6 web servers behind a router that routes the user to any of them based on server load etc.

To verify that the router is hitting all 6 servers, you need to open the site, verify the IP addrss, close the browser, re-open the browser, hit the site verify address, close browser, etc...

I'm trying to write a monitoring application that will do this for us, but I cannot get the application to simulate that browser close event that clears the channel and forces the router to forget where it sent me last time.

I have a base form that makes a call to a VB class that does the HTTPWebRequest, in the "on-click" event of a button on the form, it creates a new instance of the request class, makes the request, then disposes of the request class.

I'm not sure if this is even possible, but I need the application to simulate a browser close without having to actually close the application down...

ASP, VB, VBS, Cold Fusion, SQL, DTS, T-SQL, PL-SQL, IBM-MQ, Crystal Reports, Crystal Enterprise
 
If your web servers are load balancing behind a router, you should always get the same IP back, unless the web page you are downloading has the IP of the server that is hosting it embedded.

Also, depending on how the router is configured, if there is not sufficient load, you may always get sent to the same server, regardless of your past browsing.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
The page itself renders the IP from which it came from. We are able to manually verify functionality, I'm trying to write a utility that will do it for us, but we need it to get the router to forget the previous route.

Closing the browser does just this when we are doing it manually, but we can't get the VB app to give the same results.

ASP, VB, VBS, Cold Fusion, SQL, DTS, T-SQL, PL-SQL, IBM-MQ, Crystal Reports, Crystal Enterprise
 
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
 
Hmm, you have a lot of extra stuff set in there. If you don't need it, strip it out. I have an app that scrapes some XML sources for me, it looks a bit more like this:

Code:
Dim Request As HttpWebRequest
Request = WebRequest.Create("URL HERE")
'This makes sure we get the actual text of the URL, not the text of the rendered URL
Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2"

Dim Response As WebResponse
Response = Request.GetResponse()

Dim str As IO.Stream = Response.GetResponseStream

The main thing that makes me suspicious of your code is that KeepAlive is set to true.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top