ASP: Sustain Remote Cookie Sessions in an ASP/VBScript

Filed under: IIS, Quick Code, VBScript — Written by Chrissy on Thursday, April 27th, 2006 @ 12:28 am

I dug up this cold from my old netnerds blog. For Googlers wondering if sustaining a remote session is possible, the answer is yes; I've sustained remote cookie sessions using both ASP & VBScript. I've provided simplified code below. It should be self explanatory. If not, drop me a comment and I'll explain it.

<%
url1 = "http://www.netnerds.net/session/login.asp"
url2 = "http://www.netnerds.net/session/controlPanel.asp"
 
data1 = "username=bobby&pass=thepass&submit=Login"
 
theCookie = httpSessionRequest(1, "POST", url1, data1, noCookie, noViewState)
finalHTML = httpSessionRequest(2, "GET", url2, nodata, theCookie, noViewState)
 
response.write finalHTML
'---------------------------------------------------------
'THE FUNCTION
'---------------------------------------------------------
 
Function httpSessionRequest(theStep, method, url, data, cookie, viewState)
  'FYI, viewstate code has been ripped out.
  'Previously, I screenscraped to get the viewstate hidden field for aspx pages.
 
  baseURL = "http://www.netnerds.net/" 'This is to fix any broken images in the output.
 
  if len(cookie) = 0 then cookie = "dummy=dummy;"
  HTTPReferrer = Trim(url)
  postVars = Trim(data)
 
    Set XMLHTTP = server.CreateObject("MSXML2.serverXMLHttp")
    XMLHTTP.open method, Trim(url), false
 
      if UCASE(method) = "POST" Then
        XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
      End If
      XMLHTTP.SetRequestHeader "Referer", HTTPReferrer 'just in case the server cares
      XMLHTTP.setRequestHeader "Cookie", "excuse the Microsoft bug"
      XMLHTTP.setRequestHeader "Cookie", cookie
 
      XMLHTTP.send postVars
 
      'wait for response
      While XMLHTTP.readyState <> 4
        XMLHTTP.waitForResponse 1000
      Wend
      strHeaders = XMLHTTP.getAllResponseHeaders()
 
      hArr = split(strHeaders,"Set-Cookie: ")
      for kk = 1 to ubound(hArr)
        theCookie = left(hArr(kk),instr(hArr(kk),"path=/")-2)
        myCookie = myCookie & " " & theCookie
      next
 
      if len(myCookie) = 0 then mycookie = cookie
      sReturn = replace(XMLHTTP.responsetext,"../",baseURL)
 
      if cint(theStep) = 1 then
        httpSessionRequest = mycookie
      elseif cint(theStep) = 2 then
        httpSessionRequest = sReturn
      elseif cint(theStep) = 3 then
        'You can add stuff here to debug
        httpSessionRequest = mycookie
        response.write theCookie & "<p>" & mycookie & "<p>" & sReturn  & "<hr /><p>"
      end if
    set XMLHTTP = nothing
END FUNCTION
%>
11 Comments   -
  • Comment by shaharyar hafeez | June 1, 2006 @ 12:57 pm

    i am shaharyar hafeez from pakistan i have used your code in article "ASP: Sustain Remote Cookie Sessions in an ASP/VBScript"

    this code works well for 2 pages. but when i want to get the third page then i cant aple to maintain session. plz help me if u can.
    i am using the following urls in the code

    url1 = "http://www.sl.universalservice.org/menu.asp"

    url2= "http://www.sl.universalservice.org/FY3_form471/ExtDisplay471_StartSearch.asp"

    url3= "http://www.sl.universalservice.org/FY3_form471/ExtDisplay471_ApplicantSearch.asp"

    data1="txt471id=504472&txtSecurityCode=&CameFrom=Current&hidErrMsg=
    &hid471ApplID=&hidSecCode=&hidCameFrom=&cmdSbmitName=Display"

    theCookie = httpSessionRequest(1, "GET", url1,nodata,noCookie, noViewState)

    theCookie = httpSessionRequest(1, "GET", url2, nodata, theCookie, noViewState)

    finalHTML = httpSessionRequest(2, "Post", url3, data1, theCookie, noViewState)

    i have tried alot but cant succeed please help me as soon as possible.

    thanks in advance

  • Comment by Chrissy | June 1, 2006 @ 4:33 pm

    Hey Shaharyar,
    Your current order is
    theCookie = httpSessionRequest(1, “GET”, url1,nodata,noCookie, noViewState)

    theCookie = httpSessionRequest(1, “GET”, url2, nodata, theCookie, noViewState)

    finalHTML = httpSessionRequest(2, “Post”, url3, data1, theCookie, noViewState)
    Try doing
    httpSessionRequest(1...
    httpSessionRequest(2...
    httpSessionRequest(3...

    instead.

  • Comment by Tahir Ali | August 4, 2006 @ 8:11 am

    I need some help regarding how to post viewstate using xmlhttp in asp 3.0

  • Comment by china boy | August 15, 2006 @ 11:59 pm

    great!

  • Comment by raymond | May 14, 2007 @ 4:52 pm

    It's not working on Trademe.co.nz.Could you help me out please?

  • Comment by Bruno | May 17, 2007 @ 3:35 pm

    Hi,

    If I understood your code you send a request for url1 and in urls the session will be created, then you call url2 and in there you can access the session created in url1.

    What if i want to create a session in the main page and call serverXMLHTTP, is there a way to do that?
    I mean, whithout using a second url?

    All I want is that the session created in the main page can be accessed in "url".
    In my case, url will not create another session.

    I tryed to pass the cookie header like this XMLHTTP.setRequestHeader "Cookie", request.ServerVariables("HTTP_COOKIE")
    but no success.

    I would apreciate any help.
    Thanks in advance.

  • Comment by Bruno | May 17, 2007 @ 3:42 pm

    To make myself more clear what I want in terms of code is:

    ========MAIN PAGE========

    Dim xmlhttp
    set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
    xmlhttp.open "GET", "myurl.asp", false
    session("myString") = "blablabla"
    xmlhttp.send ""
    response.write = xmlhttp.responseText
    set xmlhttp = nothing

    =======myurl.asp============

    response.write session("myString")

    =========================

    The thing is: blablabla doesn't get writen on main page. All I want is for this to work.

    Sorry about placing to posts for the same thing.

  • Comment by Pete D | June 18, 2007 @ 3:17 am

    Hi Bruno,

    I had a similar need and a similar problem hence me finding this page. I think I've found out why it won't work but it might not help you. Its seems that only one page can have access to a users session space at one time but you (or we) are trying to access it in both pages. As a result the second page hangs while it waits for the firstpage to finish processing and release the session but it can't as the first page is waiting for the second to finish running...dead lock. Thats whats causing this method to not work. The only way around it I've found is to switch off sessions in the first page, the calling page then the second page has access to the session space. Not perfect but it might serve your needs it did mine. You can switch it off by add this line to the top of your page:

    * If you already have a

  • Comment by Steve Shen | September 24, 2007 @ 11:14 pm

    Great script! Thanks for sharing!

    One suggestion... Since response headers may not always contain "path=/", it could be "Path=/"( with capital "P"), make the instr() not case sensitive would be a fix:

    theCookie = left(hArr(kk),instr(hArr(kk),"path=/")-2)

    change to:

    theCookie = left(hArr(kk),instr(1,hArr(kk),"path=/",1)-2)

  • Comment by Marcus Sanford | November 12, 2007 @ 9:42 am

    Hi Chrissie - I am trying to get this to work with session variables as
    well as cookies so I've got
    xmlhttp.setRequestHeader "Cookie", "LOGGED=xxxxxxxxxx"
    xmlhttp.setRequestHeader "Cookie", "MYID=xxxxxxx"

    works fine - however how do I get this to run with session variables
    is it just a case os writing
    xmlhttp.setRequestHeader "Session", "MYSession=xxxxxxx"

    any help would be really great

    Many thanks

    Marcus

  • Comment by David Luu | December 31, 2007 @ 4:22 pm

    Thanx for the info Chrissy! :)

    For anyone interested, I wrapped Chrissy's method around a slightly different friendly API for VBScript, and for Javascript as well. It might not be perfect but it's a start. The Javascript version is intended to be cross-platform.

    Web/HTTP Automation Libraries
    http://www.codeproject.com/KB/library/httpautomationlibs.aspx

    I'm thinking of doing ports for .NET and Java as well, if needed.

Leave your comment