1

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
dot.Py
  • 5,007
  • 5
  • 31
  • 52
Seraphim
  • 171
  • 1
  • 12
  • 1
    There may also be a session cookie you'd need to set. Take a look there too. – Ryan Wildry Feb 06 '17 at 16:29
  • Thank you Ryan for your suggestion. I am still having the same issue, but now you make me wonder if I need to set all the cookies - since this site has multiple cookies. How do I set all of them? – Seraphim Feb 07 '17 at 16:30
  • 1
    See this question I asked on this very topic. http://stackoverflow.com/questions/38726408/retrieve-all-cookies-from-internet-explorer – Ryan Wildry Feb 07 '17 at 16:31

1 Answers1

0

BIG THANK YOU TO RYAN WILDRY! Problem Solved.

I needed to set two specific cookies and change the actual login link - using the main domain was not working.

For anyone else who finds this post one day - Look through the cookies using the developer tools and find the ones that you need to pass on login. I personally tested them by having a message box with the response headers pop up which I compared to the response headers that I usually get when I login manually.

Seraphim
  • 171
  • 1
  • 12