【发布时间】: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