使用该MakeSessionRequest
功能,您可以检索存储在 cookie 中的会话 ID,并在所有后续请求中使用
xmlHTTP.setRequestHeader "Cookie", "<key>=<value>"
Function MakeSessionRequest(method As String, url As String, data As String, _
ByRef cookie As String, Optional ByRef updateCookie = False) As Byte()
If Len(cookie) = 0 Then cookie = "dummy=dummy;"
httpReferrer = Trim(url)
postVars = Trim(data)
Dim XMLHTTP As Object
Set XMLHTTP = 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 'in case the server cares
XMLHTTP.setRequestHeader "Cookie", "to deal with XMLHTTP bug"
XMLHTTP.setRequestHeader "Cookie", cookie
XMLHTTP.send postVars
'wait for response
While XMLHTTP.readyState <> 4
XMLHTTP.waitForResponse 1000
Wend
' extract the cookie data from the response header
If updateCookie Then
cookie = ""
strHeaders = XMLHTTP.getAllResponseHeaders()
hArr = Split(strHeaders, "Set-Cookie: ")
For kk = 1 To UBound(hArr)
theCookie = Left(hArr(kk), InStr(hArr(kk), "path=/") - 2)
cookie = cookie & " " & theCookie
Next
End If
'return the response body
MakeSessionRequest = XMLHTTP.responseBody
Set XMLHTTP = Nothing
End Function