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

Discussion in 'ASP .Net' started by =?Utf-8?B?RGF2ZSBBbHZhcmFkbw==?=, Sep 1, 2005.

  1. 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
     
    =?Utf-8?B?RGF2ZSBBbHZhcmFkbw==?=, Sep 1, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hoenes1
    Replies:
    0
    Views:
    398
    hoenes1
    Aug 16, 2004
  2. Michael Fortin
    Replies:
    1
    Views:
    5,774
    John C. Bollinger
    Oct 12, 2004
  3. Replies:
    2
    Views:
    558
    =?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=
    May 3, 2006
  4. Replies:
    1
    Views:
    656
    Hans Kesting
    Sep 19, 2008
  5. hisan
    Replies:
    1
    Views:
    1,414
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page