如何从InternetExplorer.Application访问cookie

时间:2012-03-13 19:45:29

标签: vba

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

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

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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