【问题标题】:Get Response Header Using InternetExplorer.Applicaion?使用 InternetExplorer.Application 获取响应标头?
【发布时间】:2014-07-26 13:19:19
【问题描述】:

有人可以帮助我,或者至少可以为我指明正确的方向。

我正在尝试在 VBA 中编写一个自动化脚本,该脚本将登录一个网站并点击该网站上的一些按钮和复选框,然后触发下载。

网页正在使用 ASP.net。

我可以通过使用 InternetExplorer.Applicaion 对象来实现我想要的,但问题是我使用 API 和 SendKeys 将文件保存到 SaveAs 对话框窗口,它可以工作但非常粗糙并且很容易失败并且我真的希望它能够自行运行而不会出现任何问题。

理想情况下,我希望能够使用 HTTP 请求/响应方法,但是由于站点身份验证,我已经尝试了几个小时而没有任何成功,而且我还认为它对脚本有一些保护,因为 cookie 文件具有 HttpOnly标志设置。

我要下载的文件是 text/csv 并且可以通过我可以通过 Fiddler 看到的 http 响应标头访问输出的数据。

有什么方法可以从 InternetExplorer.Applicaion 对象中获取 HTTP 响应标头,我一直在查看 MS 文档,但没有看到任何明显的方法?

这是我目前使用的代码:

Private Function GetFile() As Scripting.File

On Error Resume Next

Dim objFso As Scripting.FileSystemObject: Set objFso = New Scripting.FileSystemObject

Dim strFileName As String: strFileName = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & Second(Now()) & ".csv"
Dim strFolder As String: strFolder = objFso.GetAbsolutePathName(Environ("TEMP")) & "\"

Dim strSaveAsFullPath As String: strSaveAsFullPath = strFolder & strFileName

Dim objIE As InternetExplorer: Set objIE = New InternetExplorer

With objIE

    .Visible = True

    .Navigate2 "WEB URL 1"

    Do Until Not .Busy
        Application.Wait Now + TimeValue("00:00:01")
        DoEvents
    Loop

    .Document.getElementById("ctl02_txtUserId").value = "USER"
    .Document.getElementById("ctl02_txtPassword").value = "PASS"
    .Document.getElementById("ctl02_btnLogon").Click

    Do Until Not .Busy
        Application.Wait Now + TimeValue("00:00:01")
        DoEvents
    Loop

    .Navigate2 "WEB URL 2"

    Do Until Not .Busy
        Application.Wait Now + TimeValue("00:00:01")
        DoEvents
    Loop

    .Document.getElementById("ddlTables").selectedIndex = 8
    .Document.forms(0).submit

    Do Until Not .Busy
        Application.Wait Now + TimeValue("00:00:01")
    Loop

    .Document.getElementById("gvFields_lnkbtnSelectAll").Click

    Do Until Not .Busy
        Application.Wait Now + TimeValue("00:00:01")
        DoEvents
    Loop

    .Document.getElementById("btnRetrieveData").Click

    Do Until Not .Busy
        Application.Wait Now + TimeValue("00:00:01")
        DoEvents
    Loop

    .Document.getElementById("btnRetrieveData").Focus

'''''''''''''''''''''''''''''''''''''''''''' ''''''''''' '这是我使用 SENDKEYS 下载文件的地方 :( '我需要一个更好的解决方案吗??? ''''''''''''''''''''''''''''''''''''''''''''' ''''''''

    Application.Wait Now + TimeValue("00:00:01")
    DoEvents

    Application.SendKeys "{TAB}", True
    Application.SendKeys "{TAB}", True
    Application.SendKeys "{DOWN}", True
    Application.SendKeys "{DOWN}", True

    Application.SendKeys "a", True

    Application.Wait Now + TimeValue("00:00:01")
    DoEvents

    Application.SendKeys strSaveAsFullPath, True

    Application.SendKeys "{TAB}", True
    Application.SendKeys "{TAB}", True
    Application.SendKeys "{TAB}", True
            Application.Wait Now + TimeValue("00:00:01")
    DoEvents

    Application.SendKeys "s", True
    Application.Wait Now + TimeValue("00:00:02")
    Application.SendKeys "y", True

'''''''''''''''''''''''''''''''''''''''''''' '''''''''''

    .Quit

End With

Dim objFile As Scripting.File: Set objFile = objFso.GetFile(strSaveAsFullPath)

Set GetTransworldFile = objFile

Set objIE = Nothing
Set objFso = Nothing

结束函数

【问题讨论】:

  • 使用chrome开发者工具偷看响应头?此外,Application.Wait 不是一个很好的等待“等待”HTML 加载的方法,请参阅 thisthis 了解不同的“等待”方法。
  • 对于下载,如果CSV文件可以通过它自己的URL来识别,那么你可以使用一些WinAPI函数(urldownloadtofile)。如果它是一个普通的“另存为”对话框,而不是由 javascript 函数提供的,那么您可以使用其他 WinAPI 函数来获取对话框窗口的句柄并在没有 SendKeys 的情况下执行保存。如果它是一个函数服务的弹出窗口,我不知道任何其他方法。

标签: vba internet-explorer


【解决方案1】:

我终于弄清楚了如何做到这一点,我使用 XMLHTTP 并设置各种标题以伪装成合法的浏览器,然后我提取视图状态和事件验证并使用它来发布各种表单数据。我很高兴,我一天的大部分时间都在做这个:)

我不会发布我的代码,因为它有点乱,但是如果其他人遇到这种问题,您应该下载并使用 Fiddler 从浏览器捕获 HTTP 帖子和响应,然后使用您的脚本尝试并模拟浏览器通过设置相同的标题和表单数据,这样做应该很容易..

【讨论】:

  • 发布您的代码将使这是一个有效且有用的答案。因为这个答案的价值非常有限。很遗憾,我期待着阅读它是如何解决的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 2012-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多