1

保護された Web ページからファイルをダウンロードしようとしています (仕事のため、URL を投稿できません)。

バイト (WinHTTP 要求を使用しています) を .xls に保存すると、ファイルが破損しているように見えます。

私のコード (機密情報なし) と「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

コードが実行されます。すべてのチェック済みステータスで「OK」になりましたが、ファイル (622kb で、手動でダウンロードした場合と同じ) を開こうとすると、セルのデータが多すぎるというメッセージが表示されます。「とにかく開く」をクリックすると、いくつかの部分を読むことができます。

ファイルが別のエンコードで保存されているようです。

HTTP Header Live からのいくつかのコード。

ログイン:
https://www.website.com/retro/logincheck.asp
ホスト: www.website.com
ユーザーエージェント: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q =0.3
Accept-Encoding: gzip、deflate、br
Content-Type: application/x-www-form-urlencoded
Content-Length: 38
Origin: https://www.website.com
Connection: keep-alive
Referer: https:/ /www.website.com/retro/default.asp?idioma=ES
クッキー: cc_social=yes; cc_analytics=はい; cc_advertising=はい; cc_necessary=はい; _ga=GA1.2.859443177.1570897852; __utma=136336428.859443177.1570897852.1570897852.1570897852.1; __utmz=136336428.1570897852.1.1.utmcsr=google|utmccn=(オーガニック)|utmcmd=オーガニック| utmctr=(not%20provided); ASPSESSIONIDQWSBSQAD=LDIIIENCGKDHMEJEGKJKBDMM; cookieconsent=却下; ASPSESSIONIDQWQCRRBD=PCOIENHDNLHCKNODMJLKAFGM
Upgrade-Insecure-Requests: 1
txtUser=MYUSER&txtpwd=MYPASS&lg=es
POST: HTTP/2.0 302 見つかった
日付: 水曜日、2019 年 12 月 4 日 22:20:17 GMT
サーバー: Microsoft-IIS/8.5
キャッシュ制御: プライベート
コンテンツ-タイプ: text/html
有効期限: 2019 年 12 月 4 日水曜日 22:20:18 GMT
場所: retro.asp
コンテンツの長さ: 130
経由: 2.0 www.website.com
X-Firefox-Spdy: h2

ファイル:
https://www.website.com/retro/VerBorderoGRxls.asp?id=27348&p=3º Trimestre 2019&n=0&m=UNKNOWN&con=CIRCULAR&fmt=xls
ホスト: www.website.com
ユーザーエージェント: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9, / ;q=0.8
Accept-Language: es-ES,es ;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate, br
接続: キープアライブ
リファラー: https://www.website.com/retro/borderos_resumen.asp
クッキー: cc_social=yes; cc_analytics=はい; cc_advertising=はい; cc_necessary=はい; _ga=GA1.2.859443177.1570897852; __utma=136336428.859443177.1570897852.1570897852.1570897852.1; __utmz=136336428.1570897852.1.1.utmcsr=google|utmccn=(オーガニック)|utmcmd=オーガニック| utmctr=(not%20provided); ASPSESSIONIDQWSBSQAD=LDIIIENCGKDHMEJEGKJKBDMM; cookieconsent=却下; ASPSESSIONIDQWQCRRBD=PCOIENHDNLHCKNODMJLKAFGM
アップグレード-安全でない-リクエスト: 1

GET: HTTP/2.0 200 OK
日付: 水曜日、2019 年 12 月 4 日 22:21:40 GMT
サーバー: Microsoft-IIS/8.5
キャッシュ制御: プライベート
コンテンツ タイプ: アプリケーション/x-msexcel
有効期限: 水曜日、2019 年 12 月 4 日 22: 21:40 GMT
content-length: 637440
via: 2.0 www.website.com
X-Firefox-Spdy: h2

4

0 に答える 0