I have an excel file that download historical prices from yahoo finances (I usually download last 10 years) for stocks.
However, since a couple of days, I have an issue and it seems it is coming from the cookie. When I run the program I have the below message (sorry it is in French)
I can remove them but then the program only download the last 8 months.
Dim crumb As String
Dim cookie As String
Dim response As String
Dim strURL As String
Dim DownloadURL As String
Dim period1, period2 As Double
Dim startdate2 As Date
Dim httpReq As WinHttp.WinHttpRequest
Set httpReq = New WinHttp.WinHttpRequest
Application.ScreenUpdating = False
DownloadURL = "https://finance.yahoo.com/lookup?s=" & stockSymbol
startdate2 = DateValue("01/01/1970")
period1 = CDbl((startDate - startdate2) * 86400)
period2 = CDbl((endDate - startdate2) * 86400)
With httpReq
.Open "GET", DownloadURL, False
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"
.send
.waitForResponse
response = .responseText
cookie = Split(.getResponseHeader("Set-cookies"), ";")(0)
End With
Dim counter As Long
Dim startCounter As Long
Dim result As String
crumb = Chr(34) & "CRUMB" & Chr(34) & ":{" & Chr(34) & ".crumb" & Chr(34) & ":" & Chr(34)
startCounter = InStr(response, crumb) + Len(crumb)
While Mid(response, startCounter, 1) <> Chr(34)
result = result & Mid(response, startCounter, 1)
startCounter = startCounter + 1
Wend
crumb = result
DownloadURL = "https://query1.finance.yahoo.com/v7/finance/download/" & stockSymbol & "?period1=" & period1 & "&period2=" & period2 & "&interval=1d&events=history&crumb=" & crumb
With httpReq
.Open "GET", DownloadURL, False
.setRequestHeader "Cookie", cookie
.setRequestHeader "strict-transport-security", "max-age=0"
.send
Debug.Print .getAllResponseHeaders
.waitForResponse
dataresult = .responseText
Debug.Print dataresult
End With
dataresult = Replace(dataresult, ",", vbTab)
Thank you in advance for your help :)