2013-11-03 2 views
0

웹 사이트를 여러 사용자 로그인으로 연결하려고합니다.Microsoft.XMLHTTP의 다중 인스턴스

사이트를 연결하는 동안 세션 ID는 웹 브라우저에서 쿠키로 설정됩니다. 개체 배열을 생성하여이 문제를 해결하려고했습니다.

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

불행히도 모든 XMLHTTP 개체가 동일한 쿠키 세션 ID를 공유하는 것처럼 보입니다.

독자적인 브라우저 객체를 가질 수있는 방법이 있습니까? 쿠키를 수동으로 설정 하시겠습니까? 다른 Object를 사용하여 쿠키로 세션 ID를 관리하면서 http, Get Result를 보냈습니다.

답변

0

사용 쿠키에 저장되어있는 세션 ID를 검색하고 모든 후속 요청을 서버에 전달할 수있는 MakeSessionRequest 기능을 사용

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