0

複数のユーザー ログインを使用して Web サイトに接続しようとしています。

サイトに接続している間、セッション ID は Web ブラウザで Cookie として設定されます。オブジェクトの配列を生成することでこれを解決しようとしました。

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

残念ながら、すべての XMLHTTP オブジェクトが同じ Cookie セッション ID を共有しているようです。

独立した Browser Objects を持つ方法はありますか? または、クッキーを手動で設定しますか? または、別の Object を使用して HTTP を送信し、Cookie で Session-ID を管理しながら結果を取得します。

4

1 に答える 1

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
于 2013-11-04T04:19:16.360 に答える