By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.