VBA Microsoft.XMLHTTP setRequestHeader 不发送 cookie

jeo*_*eon 5 cookies excel vba http

我的 VBA 代码发送除 Cookie 信息之外的每个标头。

Dim oXMLHttpRequest As Object
Set oXMLHttpRequest = CreateObject("Microsoft.XmlHttp")
oXMLHttpRequest.setRequestHeader "Accept", "text/html, application/xhtml+xml, */*"
oXMLHttpRequest.setRequestHeader "Accept-Language", "ko-KR"
oXMLHttpRequest.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
oXMLHttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oXMLHttpRequest.setRequestHeader "Accept-Encoding", "gzip, deflate"
oXMLHttpRequest.setRequestHeader "Connection", "Keep-Alive"
oXMLHttpRequest.setRequestHeader "DNT", "1"
oXMLHttpRequest.setRequestHeader "Cookie", "xxx=yyy"
oXMLHttpRequest.send[enter image description here][1]
Run Code Online (Sandbox Code Playgroud)

正如您在下面的捕获链接中看到的那样,缺少 Cookie: xxx=yyy .. 我不知道。请帮我。谢谢你。

提琴手捕获:

提琴手捕获

Rob*_*zie 5

Google上有传言说您必须在此处使用 WinHTTP 对象而不是 MSXML2。例如:

Option Explicit

    Sub Test()

        Dim objRequest As Object
        Dim strResponse As String
        Dim blnAsync  As Boolean

        Set objRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
        blnAsync = True

        With objRequest
            .Open "POST", "http://www.comparity.net/perl/form.pl", blnAsync
            .setRequestHeader "Cookie", "timtam=penguin"
            .send
            .WaitForResponse
            strResponse = .responseText
            Debug.Print strResponse
            Debug.Print .Status
        End With

    End Sub
Run Code Online (Sandbox Code Playgroud)

  • 这不仅仅是谣言。出于安全原因,MSXML 会剥离响应并删除 cookie。 (2认同)