ASP: Sustain Remote Cookie Sessions in an ASP/VBScript

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 = "https://www.netnerds.net/session/login.asp" url2 = "https://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 = "https://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 & "

" & mycookie & "

" & sReturn & "


" end if set XMLHTTP = nothing END FUNCTION %>