【问题标题】:Downloading a file with WINHTTP using POST/GET and headers (with VBA)使用 POST/GET 和标头(使用 VBA)通过 WINHTTP 下载文件
【发布时间】:2017-02-06 03:41:45
【问题描述】:

作为我之前问题的后续:Automating File Download of a link that looks like this: https://www.domain.com/TableData/TableA.csv

我正在了解有关使用 POST 和 GET 发送的标头的更多信息 - 但我远不知道我需要知道什么。

到目前为止,我的代码可用于下载文件,但上一个问题的问题仍然存在。当我在执行后打开下载的文件时,它充满了登录页面的 html,就好像凭据在 POST 登录中不起作用一样。我怀疑我的问题在于 strAuthenticate 字符串,但我不确定。

Sub SaveFileFromURL()

Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object

fileUrl = "https://www.ncci.com/Manuals/RateTableData/State/XX/XX.csv"
filePath = "C:\Apps\information.csv"

myuser = "xxxxxx"
mypass = "xxxxxx"

strAuthenticate = "sm_userid=xxxxx&sm_password=xxxxxx"

Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")

WHTTP.Open "POST", "https://www.ncci.com", False
WHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.send strAuthenticate
x = WHTTP.getResponseHeader("Set-Cookie")

MsgBox x
WHTTP.Open "GET", fileUrl, False
WHTTP.setRequestHeader "Cookie", x
WHTTP.send



FileData = WHTTP.responseBody
Set WHTTP = Nothing

FileNum = FreeFile
Open filePath For Binary Access Write As #FileNum
    Put #FileNum, 1, FileData
Close #FileNum

MsgBox "File has been saved!", vbInformation, "Success"

End Sub

【问题讨论】:

  • 您可能还需要设置一个会话 cookie。也看看那里。
  • 感谢瑞恩的建议。我仍然有同样的问题,但现在你让我想知道我是否需要设置所有的 cookie - 因为这个网站有多个 cookie。我如何设置所有这些?
  • 查看我就这个主题提出的这个问题。 stackoverflow.com/questions/38726408/…

标签: html vba http-headers


【解决方案1】:

非常感谢瑞恩·威德瑞!问题解决了。

我需要设置两个特定的 cookie 并更改实际的登录链接 - 使用主域不起作用。

对于有一天发现此帖子的任何其他人 - 使用开发人员工具查看 cookie 并找到您需要在登录时传递的那些。我亲自测试了它们,通过弹出一个带有响应标头的消息框,我将其与手动登录时通常得到的响应标头进行了比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 2011-10-11
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多