2.1.21.8 okutil_http_download
Description
Download from given URL to a file, with https(SSL) support
Syntax
int okutil_http_download( LPCSTR lpcszURL, LPCSTR lpcszFileName, int nConnectTimeout = 0, int nResponseTimeout = 0, BOOL bIgnoreCertError = FALSE, BOOL bWait = TRUE, BOOL bShowMsg = FALSE )
Parameters
- lpcszURL
- [input]The URL specify the target file to download
- lpcszFileName
- [input]The local file to receive data downloaded from lpcszURL
- nConnectionTimeout
- [input]The number of seconds to specify timeout limit when connect to server
- nResponseTimeout
- [input]The number of seconds to specify timeout limit waiting for response of a request
- bIgnoreCertError
- [input]Ignore Error
- bWait
- [input]Set as FALSE to download in a separate thread.
- bShowMsg
- [input]True to dump message to Messages Log after download
Return
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.
Examples
EX1
- this example will download a CSV file and import to worksheet
//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
- this example shows Origin downloads file in another thread, meanwhile you can use Origin to do other things.
//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);
}
Remark
See Also
Header to Included
origin.h
Reference
|