Microsoft.XMLHTTP的多个实例

时间:2013-11-03 20:24:24

标签: vb.net excel-vba vba excel

我尝试将网站与多个用户登录相关联。

在连接站点时,会话ID在Web浏览器中设置为cookie。 我试图通过生成一个Object数组来解决这个问题。

For i = 1 To Cnz
  Set oHttp(i) = CreateObject("Microsoft.XMLHTTP")
Next i

不幸的是,所有XMLHTTP对象看起来都共享相同的Cookie会话ID。

有没有办法拥有独立的浏览器对象? 或手动设置cookie? 或者使用其他对象在使用cookie管理Session-ID时发送http,获取结果。

1 个答案:

答案 0 :(得分:0)

使用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