vba 如何从 InternetExplorer.Application 访问 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9690947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:30:27  来源:igfitidea点击:

How can I access cookie from InternetExplorer.Application

vba

提问by ltfishie

Is it possible to access response header from InternetExplorer.Application in VB/VBA/VB.net?

是否可以从 VB/VBA/VB.net 中的 InternetExplorer.Application 访问响应头?

myIE = CreateObject("InternetExplorer.Application")
    .Visible = False
    .Navigate "http://someserver/resources/postrequest"

Give this code, how would i get a hold of the header, or more specifically, the cookie.

给这个代码,我将如何获得标题,或者更具体地说,cookie。

This is very useful for making web service calls using MSXML2.ServerXMLHTTP to servers that required cookie for authentication. Once you obtain the cookie it could be passed in the header for the subsequent web service calls.

这对于使用 MSXML2.ServerXMLHTTP 向需要 cookie 进行身份验证的服务器进行 Web 服务调用非常有用。获得 cookie 后,它可以在标头中传递以供后续 Web 服务调用使用。

回答by ltfishie

Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

Private Function getCookie()
    Dim myIe As Object
    Set myIe = CreateObject("InternetExplorer.Application")
    myIe.Visible = False
    myIe.Navigate "http://someurl"
    Do While myIe.Busy
        Sleep 20
    Loop
    getCookie= myIe.Document.cookie
End Function


For this to work on 64 bit, the PtrSafekeyword needs to be added to the Sleepdefinition:

要使其在 64 位上工作,PtrSafe需要将关键字添加到Sleep定义中:

Private Declare PtrSafe Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)