As a follow up to my previous question: Automating File Download of a link that looks like this: https://www.domain.com/TableData/TableA.csv
I am learning more about headers that are sent with POST and GET - but I am far from knowing what I need to know.
The code I have so far works in downloading the file, but the problem from the last question is still persisting. When I open the downloaded file after execution it is filled with html of the login page as though the credentials did not work from the POST login. I suspect my problem lies in the strAuthenticate string, but I don't know for sure.
Sub SaveFileFromURL()
Dim FileNum As Long
Dim FileData() As Byte
Dim WHTTP As Object
fileUrl = "https://www.ncci.com/Manuals/RateTableData/State/XX/XX.csv"
filePath = "C:\Apps\information.csv"
myuser = "xxxxxx"
mypass = "xxxxxx"
strAuthenticate = "sm_userid=xxxxx&sm_password=xxxxxx"
Set WHTTP = CreateObject("WinHTTP.WinHTTPrequest.5.1")
WHTTP.Open "POST", "https://www.ncci.com", False
WHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
WHTTP.send strAuthenticate
x = WHTTP.getResponseHeader("Set-Cookie")
MsgBox x
WHTTP.Open "GET", fileUrl, False
WHTTP.setRequestHeader "Cookie", x
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