Download from given URL to a file, with https(SSL) support
int okutil_http_download( LPCSTR lpcszURL, LPCSTR lpcszFileName, int nConnectTimeout = 0, int nResponseTimeout = 0, BOOL bIgnoreCertError = FALSE, BOOL bWait = TRUE, BOOL bShowMsg = FALSE )
If bWait = true, return OHTTP_E_OK for success, other negative values are error code. If bWait = false, return 0 for download starts,other value for previous download not completed. System variable @OCDL can check the download status, see EX2.
EX1
//assume active book empty void stock() { string strSymbol = "MSFT";//you need apikey if searching any other stock string strFile = GetOriginPath(ORIGIN_PATH_USER) + strSymbol + "_stock.csv"; string strURL = "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol="; strURL += strSymbol; strURL += "&apikey=demo&datatype=csv"; string result; int err = okutil_http_download(strURL, strFile); if( err != 0 ) { out_int("err = ", err); return; } Worksheet wks = Project.ActiveLayer(); ASCIMP ai; if(AscImpReadFileStruct(strFile, &ai)==0) { ai.iSubHeaderLines = 1; ai.nLongNames = 1; wks.ImportASCII(strFile, ai); Column col(wks, 0); col.SetFormat(OKCOLTYPE_DATE, "yyyy'-'MM'-'dd"); } }
EX2
//open Messages Log to see the download begin/end message void okutil_http_download_ex2() { string strName = "ECMWF_ERA-40_subset.nc"; string strFile = GetOriginPath(ORIGIN_PATH_USER) + strName; string strURL = "https://www.unidata.ucar.edu/software/netcdf/examples/" + strName; string strMsg; double err; BOOL bWait = FALSE; // download file in a separate thread BOOL bShowMsg = TRUE; // message log will show message after download complete if( okutil_http_download(strURL, strFile, 0, 0, FALSE, bWait, bShowMsg) ) { LT_get_var("@OCDL", &err); strMsg.Format("previous download not completed %.2f%%\n", err); } else { LT_execute("sec -p 1"); //wait @OCDL update LT_get_var("@OCDL", &err); if( err < 0 ) { strMsg.Format("err = %g\n", err); } else if( err == 0 ) { strMsg.Format("download complete: %s\n", strName); } else { strMsg.Format("downloading %.2f%%\n", err); } } strMsg.Write(WRITE_MESSAGE_LOG); }
origin.h