Public Sub WinHttp_Login_Download_File1()
Dim HTMLdoc As HTMLDocument
Dim imgs As IHTMLElementCollection, imgExcel As HTMLImg, i As Long
Dim downloadURL As String
Dim httpReq As WinHttpRequest
Set httpReq = New WinHttpRequest
'Dim httpReq As Object
'Set httpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
'#If VBA7 Then
' Dim httpReq As WinHttpRequest60
' Set httpReq = New WinHttpRequest60
'#Else
' Dim httpReq As WinHttpRequest
' Set httpReq = New WinHttpRequest
'#End If
Dim fileNum As Integer, Buffer() As Byte
Dim saveInFolder As String, localFile As String
Dim res As Variant
Dim JSESSIONID As String
Dim formData As String
'Folder in which the downloaded file will be saved
saveInFolder = ThisWorkbook.Path
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
localFile = saveInFolder & "Excel workbook.xls"
'Log in - look at Fiddler request to see how username & password are sent
Set HTMLdoc = New HTMLDocument
formData = "username=xxxx&password=yyyy&otherparam=value" 'just guessing - look at Fiddler
With httpReq
.Open "POST", "https://www.loginpage.com", False 'probably a POST request with form data - see Fiddler
.setRequestHeader "Accept-Language", "fr-CA" 'plus other headers - see Fiddler
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.setRequestHeader "Accept-Encoding", "gzip, deflate" 'try with and without this
.send (formData)
Debug.Print .Status, .statusText
Debug.Print .getAllResponseHeaders
JSESSIONID = Split(.getResponseHeader("Set-Cookie"), ";")(0)
'Put response in HTMLDocument for parsing - you may need to extract params/values for use in later requests
HTMLdoc.body.innerHTML = .responseText
'To check if successfully logged in, search for something which appears on the IE page when you are logged in, e.g. "Welcome user1234", or a "Logout" link
If InStr(1, .responseText, "Welcome user1234", vbTextCompare) > 0 Then
MsgBox "Logged in"
Else
MsgBox "Not logged in"
End If
End With
'Get page with download link
With httpReq
.Open "GET", "https://www.pagewithdownloadlink.com", False
.setRequestHeader "Accept-Language", "fr-CA" 'plus other headers - see Fiddler request
.setRequestHeader "Accept-Encoding", "gzip, deflate" 'try with and without this
.setRequestHeader "Cookie", JSESSIONID
.send
Debug.Print .Status, .statusText
Debug.Print .getAllResponseHeaders
'Put response in HTMLDocument to extract the download link
HTMLdoc.body.innerHTML = .responseText
End With
'Find the "Excel Export" img tag
Set imgs = HTMLdoc.getElementsByTagName("IMG")
i = 0
Set imgExcel = Nothing
While i < imgs.Length And imgExcel Is Nothing
If imgs(i).Title = "Excel Export" Then Set imgExcel = imgs(i)
i = i + 1
Wend
If Not imgExcel Is Nothing Then
downloadURL = imgExcel.parentElement.href
With httpReq
'======= EITHER =========
'DISABLE AUTOMATIC HTTP 302 REDIRECT AND GET REDIRECT URL
.Open "GET", downloadURL, False
'.setRequestHeader "Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/xxxf, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
'.setRequestHeader "Accept-Language", "fr-CA"
.setRequestHeader "Accept-Encoding", "gzip, deflate" 'try with and without this
'.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
'.setRequestHeader "Referer", "https://www.pagewithdownloadlink" '? check Fiddler request
.setRequestHeader "Cookie", JSESSIONID
'Disable automatic http redirect, so we can extract the redirect URL from the response
.Option(WinHttpRequestOption_EnableRedirects) = False
.send
If .Status = 302 Then
'Get the redirect URL from the Location header
downloadURL = .getResponseHeader("Location")
'Send GET to (hopefully) download the Excel file
.Open "GET", downloadURL
.send
'If successful, save response in the local file
If .Status = 200 Then
fileNum = FreeFile
Open localFile For Binary Access Write As #fileNum
Buffer = .responseBody
Put #fileNum, , Buffer
Close #fileNum
MsgBox "Downloaded " & localFile
Else
MsgBox "URL = " & downloadURL & vbNewLine & "http request returned status " & .Status & vbNewLine & .statusText
End If
Else
'Not the expected 302 response
MsgBox "URL = " & downloadURL & vbNewLine & "Expected response = 302" & vbNewLine & "Actual response = " & .Status & vbNewLine & .statusText
End If
'========== END ===============
'========== OR ===============
'ALLOW AUTOMATIC REDIRECT
.Open "GET", downloadURL, False
'.setRequestHeader "Accept", "application/x-ms-application, image/jpeg, application/xaml+xml, image/xxxf, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"
'.setRequestHeader "Accept-Language", "fr-CA"
.setRequestHeader "Accept-Encoding", "gzip, deflate" 'try with and without this
'.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)"
'.setRequestHeader "Referer", "https://www.pagewithdownloadlink" '? check Fiddler request
.setRequestHeader "Cookie", JSESSIONID
.send
'If successful, save response in the local file
If .Status = 200 Then
fileNum = FreeFile
Open localFile For Binary Access Write As #fileNum
Buffer = .responseBody
Put #fileNum, , Buffer
Close #fileNum
MsgBox "Downloaded " & localFile
Else
MsgBox "URL = " & downloadURL & vbNewLine & "http request returned status " & .Status & vbNewLine & .statusText
End If
'========== END ===============
End With
Else
MsgBox "Unable to find IMG tag with title=""Excel Export"""
End If
End Sub