VBA WinHTTP 从受密码保护的 https 网站下载文件
Posted
技术标签:
【中文标题】VBA WinHTTP 从受密码保护的 https 网站下载文件【英文标题】:VBA WinHTTP to download file from password proteced https website 【发布时间】:2014-03-29 21:43:24 【问题描述】:我正在尝试使用 WinHTTP 从受 https 密码保护的站点保存文件。代码如下:
Sub SaveFileFromURL()
Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object
fileUrl = "https://www.website.com/dir1/dir2/file.xls"
filePath = "C:\myfile.xls"
myuser = "username"
mypass = "password"
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "GET", fileUrl, False
WHTTP.SetCredentials myuser, mypass, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
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
问题在于身份验证。该文件正在保存,但是当我在 Excel 中打开它时,它只是 html 登录页面,而不是实际文件。如果我复制直接文件 url 并将其粘贴到浏览器地址栏中,并且我没有登录网页,效果是一样的。我看到了登录页面。然后,如果我输入我的登录名和密码,将显示下载窗口,允许我保存文件。
所以我认为代码的 SetCredentials 部分无法正常工作,因为如果我 debug.print WHTTP.ResponseBody 它是 html 代码而不是实际文件数据。
有没有办法将用户名和密码传递给 WinHTTP,以便我能够正确保存文件?
这是页面地址:
https://sst.msde.state.md.us/
========================编辑:====================== ==
所以我今天玩了一点,我想我正在前进。这就是我得到的。我修改了这样的代码:
Sub SaveFileFromURL()
Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object
fileUrl = "https://www.website.com/dir1/dir2/file.xls"
filePath = "C:\myfile.xls"
myuser = "username"
mypass = "password"
strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "POST", fileUrl, False
WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.Send strAuthenticate
WHTTP.Open "GET", fileUrl, False
WHTTP.Send
Debug.Print WHTTP.GetAllResponseHeaders()
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
当我 Debug.Print WHTTP.GetAllResponseHeaders() 我得到例如:
Accept-Ranges: bytes
Content-Disposition: attachement; filename="xxx"
Content-Length: xxxxxx
Content-Type: application/octet-stream
所以我认为身份验证有效,但我仍然无法保存文件。当我继续:
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
FileNum = FreeFile
Open filePath For Binary Access Write As #FileNum
Put #FileNum, 1, FileData
Close #FileNum
保存的文件内容是html网页本身,而不是文件。
我是否正确地进行了身份验证,问题在于将文件保存到磁盘还是身份验证仍然存在问题,这就是为什么我无法保存它?有什么线索吗?
【问题讨论】:
SetCredentials 似乎不太可靠。您可以改用 POST/GET 方法。 在哪里/如何使用 POST/GET 方法?我在设置凭据之前在我的代码中使用它,但它不起作用。有什么建议吗? @user2267971 这个答案:***.com/questions/891427/… 可能会有所帮助。除此之外,您还可以通过 IE 自动登录页面,提交您的用户 ID 和密码,然后导航到该页面以获取您需要的信息。 我已经自动登录以首先获取直接文件 url。但我希望能够“静默”地下载文件,而不会弹出任何下载窗口。获取 WHTTP.ResponseBody 非常适用于其他非密码保护站点以及一些密码保护的 http 站点。问题出在这个特定的 https 站点上。顺便说一句,我必须为此使用 IE。 编辑:查看我提供的网站的 html 代码是否可以弄清楚必须使用什么方法来传递用户名和密码并正确下载文件? 【参考方案1】:好的,我做到了。代码如下:
Sub SaveFileFromURL()
Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object
mainUrl = "https://www.website.com/"
fileUrl = "https://www.website.com/dir1/dir2/file.xls"
filePath = "C:\myfile.xls"
myuser = "username"
mypass = "password"
'@David Zemens, I got this by examining webpage code using Chrome, thanks!
strAuthenticate = "start-url=%2F&user=" & myuser & "&password=" & mypass & "&switch=Log+In"
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
'I figured out that you have to POST authentication string to the main website address not to the direct file address
WHTTP.Open "POST", mainUrl, False 'WHTTP.Open "POST", fileUrl, False
WHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.Send strAuthenticate
'Then you have to GET direct file url
WHTTP.Open "GET", fileUrl, False
WHTTP.Send
FileData = WHTTP.ResponseBody
Set WHTTP = Nothing
'Save the file
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
感谢您的所有帮助。
顺便说一句,我发现这篇文章非常有用:
http://www.mrexcel.com/forum/excel-questions/353006-download-file-excel.html
Not understanding why WinHTTP does NOT authenticate certain HTTPS resource
How to parse line by line WinHTTP response: UTF-8 encoded CSV?
【讨论】:
以上是关于VBA WinHTTP 从受密码保护的 https 网站下载文件的主要内容,如果未能解决你的问题,请参考以下文章