gpt4 book ai didi

excel - VBA WinHTTP从受密码保护的https网站下载文件

转载 作者:行者123 更新时间:2023-12-02 09:23:34 25 4
gpt4 key购买 nike

我正在尝试使用 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网页本身,而不是文件本身。

我是否进行了正确的身份验证,但问题在于将文件保存到磁盘,或者身份验证仍然存在问题,这就是为什么我无法保存它?有什么线索吗?

最佳答案

好的,我做到了。代码如下:

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?

关于excel - VBA WinHTTP从受密码保护的https网站下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22051960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com