Hi Rajeev.
Try this, it finally works for me
:
Code: Select all
#include "HMG.CH"
FUNCTION main()
SET DATE BRIT
SET CENTURY ON
DEFINE WINDOW testunzip MAIN AT 0,0 WIDTH 200 HEIGHT 200
DEFINE DATEPICKER datepicker_1
ROW 20
COL 20
WIDTH 100
HEIGHT 20
ONLOSTFOCUS getDay()
VALUE CTOD('17/12/2015')
END DATEPICKER
DEFINE BUTTON Button_1
ROW 100
COL 20
WIDTH 100
HEIGHT 30
CAPTION "Download"
ACTION Download()
END BUTTON
DEFINE LABEL Lbl_1
ROW 45
COL 20
WIDTH 150
HEIGHT 20
VALUE ""
END LABEL
END WINDOW
testunzip.center
testunzip.activate
RETURN nil
FUNCTION Download()
LOCAL cBhavFile, cFileNm, cUrl, cFile, dDate, cHost
dDate := testunzip.datepicker_1.value
cBhavFile := 'cm' + ntoc( DAY( dDate ), 10, 2, '0' ) + UPPER( LEFT( CMONTH( dDate ), 3 ) ) + ntoc( YEAR( dDate ), 10, 4, '0' ) + 'bhav.csv'
cFileNm := cBhavFile + '.zip'
cHost:= "https://www1.nseindia.com"
cUrl := "/ArchieveSearch?h_filetype=eqbhav&date=" + hb_DtoC( dDate , "dd-mm-YYYY" ) + "§ion=EQ"
cFile := getcurrentfolder() + '\' + cFileNm
lGotFile := downloadfile( cHost, cUrl, cFile, dDate, "NSE" )
IF !lGotFile
msginfo( {"NSE Bhavcopy file not found for ", dDate })
ELSE
IF FILE( cFile )
UNCOMPRESS cFile
msgdebug("File uncompressed")
ENDIF
ENDIF
RETURN nil
FUNCTION GetDay()
testUnZip.Lbl_1.value := cdow(testUnZip.datepicker_1.value)
RETURN nil
****************************************************************
FUNCTION DownloadFile( cHost, cUrl, cFile, dDate, cExchange )
LOCAL ohttp, lOk
LOCAL nFileSize
Local cHeader, cHeaders, cLine, nPos, cLink, recv
Local cUrlFile:=""
Local cCookie:=""
IF FILE ( cFile ) //old file exist
DELETEFILE ( cFile )
ENDIF
lOk := .F.
nFileSize := 0
BEGIN SEQUENCE WITH { | e | BREAK( e ) }
oHttp := Win_OleCreateObject( "WinHttp.WinHttpRequest.5.1" )
// Connect to the HTTPs server
// Set Headers
ohttp:Open( "GET", cHost + cUrl, .T. )
ohttp:setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
ohttp:setRequestHeader("Accept", "*/*")
ohttp:setRequestHeader("X-Requested-With", "XMLHttpRequest")
ohttp:setRequestHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36")
ohttp:setRequestHeader("DNT", "1")
ohttp:setRequestHeader("Referer", "https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm")
ohttp:Send()
ohttp:WaitForResponse()
// download the response
recv := ohttp:responseBody()
cHeaders:=ohttp:getAllResponseHeaders()
IF ohttp:Status <> 200
BREAK "HTTP Server " + cHost + cUrl + " status is " + hb_NToS( ohttp:Status ) + " " + ohttp:statusText
ENDIF
//get cookies
cCookie:=""
FOR EACH cHeader IN hb_ATokens(cHeaders, .T.)
IF Left(cHeader,12) == "Set-Cookie: "
cCookie += IF (!EMPTY( cCookie ), '; ', '') + hb_ATokens(SubStr( cHeader, 13 ), ';') [1]
ENDIF
NEXT
* msgdebug (cCookie)
//check links
FOR EACH cLine IN hb_ATokens( recv, .T.)
nPos:=AT("<A HREF=", Upper(cLine))
IF nPos > 0
cLink:= SubsTR(cLine, nPos + Len("<A HREF=") )
nPos:=AT(" ", cLink)
IF nPos > 0
cLink:=Left(cLink, nPos - 1)
ENDIF
IF hb_FNameNameExt (cLink) == hb_FNameNameExt (cFile)
cUrlFile:= cHost + cLink
ENDIF
ENDIF
NEXT
IF EMPTY ( cUrlFile )
BREAK 'No link to the "' + hb_FNameNameExt (cFile) + '" file'
ENDIF
* msgdebug (cUrlFile)
//get file from link
ohttp:Open( "GET", cUrlFile, .T. )
ohttp:setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
ohttp:setRequestHeader("Accept", "*/*")
ohttp:setRequestHeader("X-Requested-With", "XMLHttpRequest")
ohttp:setRequestHeader("User-Agent", "Mozilla/5.0 (Linux; Android 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36")
IF !Empty( cCookie )
ohttp:setRequestHeader("Cookie", cCookie) //required if exist
ENDIF
ohttp:setRequestHeader("DNT", "1")
ohttp:setRequestHeader("Referer", "https://www.nseindia.com/products/content/equities/indices/historical_index_data.htm")
ohttp:Send()
ohttp:WaitForResponse()
// download the response
recv := ohttp:responseBody()
*cHeaders:=ohttp:getAllResponseHeaders()
IF ohttp:Status <> 200
BREAK "HTTP Server " + cUrlFile + " status is " + hb_NToS( ohttp:Status ) + " " + ohttp:statusText
ENDIF
ohttp:=Nil
IF EMPTY( recv ) .OR. '!DOCTYPE' $ recv
BREAK "Invalid response "
ENDIF
StrFile ( recv, cFile )
RECOVER USING oError
MsgStop( IF( VALTYPE( oError ) = 'O', oError:Description, oError ) )
END SEQUENCE
ohttp := Nil
lOk := FILE( cFile )
RETURN lOk
It seems to me that the problem is the use of the object "MSXML2.ServerXMLHTTP", which probably sends cookies incorrectly.
PS. The earliest file I've ever downloaded is from November 7, 1994
Best regards, Edward.