1

Making my first steps on HTTP within VBA. Already managed to get cookie value in order to make a request. The problem I'm having is that the file I want to download is not in the first response. When I analyse using HTTP Header Live, the browser receives several responses and only the last one is the file, a PDF that is generated after a query sent by the user. The only thing I'm getting is the first response that I'm displaying with a MsgBox. Can someone help me solving this problem. Made some searches through the web but haven't found yet, a solution.

The code I am using is:

Sub Test()

    Dim WinHttpReq As Object

    Set WinHttpReq = CreateObject("WinHTTP.WinHTTPrequest.5.1")
    With WinHttpReq
        .Open "POST", myURL, False ', "username", "password"
        .send
        x = .getResponseHeader("Set-Cookie")
        i = InStr(x, ";")
        x = Left(x, i - 1)
        MsgBox x
    End With
    With WinHttpReq
        .Open "GET", myURL, False
        .Option(WinHttpRequestOption_EnableRedirects) = True
        .SetRequestHeader "Content-Type", "application/pdf"
        .SetRequestHeader "Accept-Encoding", "gzip, deflate, br"
        .SetRequestHeader "Cookie", x
        .SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0"
        .send
    End With
    MsgBox (WinHttpReq.getAllResponseHeaders())
    If WinHttpReq.Status = 200 Then
        Set oStream = CreateObject("ADODB.Stream")
        With oStream
            .Type = 1
            .Open
            .Write WinHttpReq.responseBody
            .SaveToFile "C:\Users\xxx\Desktop\file.pdf", 2
            .Close
        End With
    End If
    Set WinHttpReq = Nothing
    Set oStream = Nothing

End Sub

Analyzing with Firefox, when I enter the URL, I receive two responses with 200 OK. I wonder how can I get the second response? The site uses a javascript that interprets the query I send and returns a PDF file. The file name changes according to the user and the query.

Now, i reached the following point. I make a first request to get the cookie, then a second to get an ETag from a diferent address. The problem is that when i make the third request to download the file, the filename is apparently generated by the server (APACHE?), based on the ETag and something I'm not being able to find. The last 5 numbers from the ETag change in the filename. For example: ETag - 1543932096000 File - 1543932095115.xxxxx.address.com.5245.idp.pdf Since I don't have the filename, i cannot download the file with an httprequest. Help?

  • 2
    Please include your code. URL if possible. – QHarr Nov 10 '18 at 13:07
  • To handle redirection you need switch redirections off `.Option(6) = False`, then `.Send`, and check redirect URL `.getResponseHeader("Location")`. Make second request using retrieved redirection URL and process response. Also do `CreateObject("WinHTTP.WinHTTPrequest.5.1")` each time before `.Open` – omegastripes Nov 20 '18 at 20:06
  • Thanks. Tried but it still doesn't work. The first response has no location, only cookie. – Filipe Brun Machado Nov 21 '18 at 17:35

0 Answers0