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.
Example
1<%
2url1 = "https://www.netnerds.net/session/login.asp"
3url2 = "https://www.netnerds.net/session/controlPanel.asp"
4
5data1 = "username=bobby&pass=thepass&submit=Login"
6
7theCookie = httpSessionRequest(1, "POST", url1, data1, noCookie, noViewState)
8finalHTML = httpSessionRequest(2, "GET", url2, nodata, theCookie, noViewState)
9
10response.write finalHTML
11%>
Function
1<%
2'---------------------------------------------------------
3' THE FUNCTION
4'---------------------------------------------------------
5
6Function httpSessionRequest(theStep, method, url, data, cookie, viewState)
7 ' FYI: viewstate code has been ripped out.
8 ' Previously, I screenscraped to get the viewstate hidden field for aspx pages.
9
10 baseURL = "https://www.netnerds.net/" ' This is to fix any broken images in the output.
11
12 If Len(cookie) = 0 Then cookie = "dummy=dummy;"
13 HTTPReferrer = Trim(url)
14 postVars = Trim(data)
15
16 Set XMLHTTP = Server.CreateObject("MSXML2.serverXMLHttp")
17 XMLHTTP.open method, Trim(url), False
18
19 If UCase(method) = "POST" Then
20 XMLHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
21 End If
22 XMLHTTP.SetRequestHeader "Referer", HTTPReferrer ' just in case the server cares
23 XMLHTTP.SetRequestHeader "Cookie", "excuse the Microsoft bug"
24 XMLHTTP.SetRequestHeader "Cookie", cookie
25
26 XMLHTTP.send postVars
27
28 ' wait for response
29 While XMLHTTP.readyState <> 4
30 XMLHTTP.waitForResponse 1000
31 Wend
32
33 strHeaders = XMLHTTP.getAllResponseHeaders()
34
35 hArr = Split(strHeaders, "Set-Cookie: ")
36 For kk = 1 To UBound(hArr)
37 theCookie = Left(hArr(kk), InStr(hArr(kk), "path=/") - 2)
38 myCookie = myCookie & " " & theCookie
39 Next
40
41 If Len(myCookie) = 0 Then myCookie = cookie
42 sReturn = Replace(XMLHTTP.responseText, "../", baseURL)
43
44 If CInt(theStep) = 1 Then
45 httpSessionRequest = myCookie
46 ElseIf CInt(theStep) = 2 Then
47 httpSessionRequest = sReturn
48 ElseIf CInt(theStep) = 3 Then
49 ' You can add stuff here to debug
50 httpSessionRequest = myCookie
51 Response.Write theCookie & vbCrLf & myCookie & vbCrLf & sReturn & vbCrLf & "* * *"
52 End If
53
54 Set XMLHTTP = Nothing
55End Function
56%>