Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VBA Excel从受保护网站下载文件_Excel_Vba_Winhttprequest_Downloading Website Files_Protected Resource - Fatal编程技术网

使用VBA Excel从受保护网站下载文件

使用VBA Excel从受保护网站下载文件,excel,vba,winhttprequest,downloading-website-files,protected-resource,Excel,Vba,Winhttprequest,Downloading Website Files,Protected Resource,我正在尝试从受保护的网页下载文件(从我的工作中,因此我无法发布url) 当我将字节(我使用的是WinHTTP请求)保存在.xls中时,文件似乎已损坏 我的代码(没有敏感信息)和“HTTP Header Live”使用的一些代码 代码运行。我在所有选中状态下都得到了“OK”,但当我尝试打开该文件时(该文件有622kb,与手动下载时相同),Excel会提示我一条消息,说明单元格中的数据太多。如果我点击“无论如何打开”,我可以阅读一些部分 看起来文件是以不同的编码保存的 HTTP头中的一些代码处于活动

我正在尝试从受保护的网页下载文件(从我的工作中,因此我无法发布url)

当我将字节(我使用的是WinHTTP请求)保存在.xls中时,文件似乎已损坏

我的代码(没有敏感信息)和“HTTP Header Live”使用的一些代码

代码运行。我在所有选中状态下都得到了“OK”,但当我尝试打开该文件时(该文件有622kb,与手动下载时相同),Excel会提示我一条消息,说明单元格中的数据太多。如果我点击“无论如何打开”,我可以阅读一些部分

看起来文件是以不同的编码保存的

HTTP头中的一些代码处于活动状态

option explicit

Sub SaveFileFromURL()

Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object
Dim SetCookieString As String
Dim mainUrl As String
Dim fileUrl As String
Dim SavePath As String
Dim strAuthenticate As String

'this is the url to login, extracted with HTTP Header Live.
'however, the url shown in the browser is: "https://www.website.com/retro/default.asp?idioma=ES"
mainUrl = "https://www.website.com/retro/logincheck.asp"

'this is the url for to download the file, but need to send a cookie as credentials
'(if you login via browser and paste the link, it will popup the saveas dialog, but if you don't login, it will return an internal error)
fileUrl = "https://www.website.com/retro/VerBordero.asp?id=27348&p=3º Trimestre 2019&n=0&m=UNKNOWN&con=CIRCULAR&fmt=xls"

'the path where to save the file with the extension I know it will have.
SavePath = "C:\Users\Victor\Desktop\bordero.xls"

'authetication should be:    
strAuthenticate = "txtUser=MYUSER&txtpwd=MYPASS&lg=es"

'I login to catch the cookie that it is suppose to allow me to download the file.
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "POST", mainUrl, False
WHTTP.Send strAuthenticate

'if it is correct:
If WHTTP.Status = 200 Then

    'I get the cookie
    SetCookieString = WHTTP.getResponseHeader("Set-Cookie")

    'Then you have to GET direct file url
    WHTTP.Open "GET", fileUrl, False
    WHTTP.setRequestHeader "Content-Type", "application/x-msexcel"
    WHTTP.setRequestHeader "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    WHTTP.setRequestHeader "Accept-Language", "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3"
    WHTTP.setRequestHeader "Cookie", SetCookieString
    WHTTP.Send

    'if i get the file
    If WHTTP.Status = 200 Then

        'download bytes
        FileData = WHTTP.responseBody
        Set WHTTP = Nothing

        'Save the bytes into file
        FileNum = FreeFile
        Open SavePath For Binary Access Write As #FileNum
            Put #FileNum, 1, FileData
        Close #FileNum

    End If

End If

End Sub
登录:

主持人:www.website.com
用户代理:Mozilla/5.0(Windows NT 6.1;Win64;x64;rv:70.0)Gecko/20100101 Firefox/70.0
接受:text/html、application/xhtml+xml、application/xml;q=0.9,/;q=0.8
接受语言:es,es;q=0.8,在美国;q=0.5,en;q=0.3
接受编码:gzip,deflate,br
内容类型:application/x-www-form-urlencoded
内容长度:38
来源:
连接:保持活动状态
推荐人:
Cookie:cc_social=yes;cc_analytics=是;cc_广告=是;cc_必要=是_ga=GA1.2.859443177.1570897852__utma=136336428.859443177.1570897852.1570897852.1570897852.1__utmz=136336428.1570897852.1.1.utmcsr=google | UTMCN=(有机)| utmcmd=organic | utmctr=(未提供%);ASPSESSIONIDQWSBSQAD=LDIIIENCGKDHMEJEGKJKBDMM;cookieconsent=解雇;ASPSESSIONIDQWQCRRBD=PCOIENHDNLHCKNODMJLKAFGM
升级不安全的请求:1
txtser=MYUSER&txtpwd=MYPASS&lg=es
POST:HTTP/2.0 302已找到
日期:2019年12月4日星期三22:20:17 GMT
服务器:Microsoft IIS/8.5
缓存控制:专用
内容类型:文本/html
过期时间:2019年12月4日星期三22:20:18 GMT
位置:retro.asp
内容长度:130
via:2.0 www.website.com
X-Firefox-Spdy:h2

文件:
2019年三季度&n=0&m=未知&con=循环&fmt=xls
主持人:www.website.com
用户代理:Mozilla/5.0(Windows NT 6.1;Win64;x64;rv:70.0)Gecko/20100101 Firefox/70.0
接受:text/html、application/xhtml+xml、application/xml;q=0.9,/;q=0.8
接受语言:es,es;q=0.8,在美国;q=0.5,en;q=0.3
接受编码:gzip,deflate,br
连接:保持活动状态
推荐人:
Cookie:cc_social=yes;cc_analytics=是;cc_广告=是;cc_必要=是_ga=GA1.2.859443177.1570897852__utma=136336428.859443177.1570897852.1570897852.1570897852.1__utmz=136336428.1570897852.1.1.utmcsr=google | UTMCN=(有机)| utmcmd=organic | utmctr=(未提供%);ASPSESSIONIDQWSBSQAD=LDIIIENCGKDHMEJEGKJKBDMM;cookieconsent=解雇;ASPSESSIONIDQWQCRRBD=PCOIENHDNLHCKNODMJLKAFGM
升级不安全的请求:1

GET:HTTP/2.0 200正常
日期:2019年12月4日星期三22:21:40 GMT
服务器:Microsoft IIS/8.5
缓存控制:专用
内容类型:应用程序/x-msexcel
过期时间:2019年12月4日星期三22:21:40 GMT
内容长度:637440
via:2.0 www.website.com
X-Firefox-Spdy:h2