Keeping a session alive when accessing an ASP page with .Net code

G

Guest

I'm working on some code that gathers data from a web interface to one of our
servers (classic ASP). The problem is that the site uses session variables
to store login information, so to move from page to page I need to emulate a
browser keeping a session active.

I'm currently using the .Net code below to access each web page. If I get
back an ASPSESSIONID cookie, I store it in a Cookie object, and keep passing
that object back every time I make a new request. However, it isn't working
properly--every request generates a new session on the web server. What am I
doing wrong?

Function GetHTTPRequestAsStream(ByVal strURI As String, ByRef SessionCookie
As Cookie) As Stream
Dim objURI As Uri
Dim objHTTPRequest As HttpWebRequest
Dim objHTTPResponse As HttpWebResponse
Dim objStream As Stream
Dim i As Integer

objURI = New Uri(strURI)

'Make the request
objHTTPRequest = HttpWebRequest.Create(objURI)
objHTTPRequest.UserAgent = "msie 6.0"
'If we have a session cookie, use it
If Not SessionCookie Is Nothing Then
objHTTPRequest.CookieContainer = New CookieContainer
objHTTPRequest.CookieContainer.Add(SessionCookie)
End If

objHTTPResponse = objHTTPRequest.GetResponse()

'If we didn't have a session cookie, grab the one that was returned
If SessionCookie Is Nothing Then

SessionCookie = New Cookie
For i = 0 To objHTTPResponse.Headers.Count - 1
If InStr(objHTTPResponse.Headers(i), "ASPSESSION") > 0 Then
SessionCookie.Name =
objHTTPResponse.Headers(i).Split(";")(0).Split("=")(0)
SessionCookie.Value =
objHTTPResponse.Headers(i).Split(";")(0).Split("=")(1)
SessionCookie.Domain = strURI
SessionCookie.Path =
objHTTPResponse.Headers(i).Split(";")(1).Split("=")(1)
End If
Next

End If

objStream = objHTTPResponse.GetResponseStream
GetHTTPRequestAsStream = objStream

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top