download file with Wininet

 

Hi guys. I am trying to write an EA which will auto download a file from the internet, then read data from that file and then execute trade using those data. but i encounter problem with downloading file. the file i download got a bigger size compare to the one on the server.

If I run this code on Windows 7, then the file downloaded will have the correct file size. but if i run the EA on my Windows Server 2012 R2 then the file downloaded will have different file size compared to the file on server (the data inside the file keep repeating itself).

What am i doing wrong? 

my code:

#define READURL_BUFFER_SIZE   100000

#import  "Wininet.dll"
int InternetOpenW(string,int,string,string,int);
int InternetConnectW(int,string,int,string,string,int,int,int);
int HttpOpenRequestW(int,string,string,int,string,int,string,int);
int InternetOpenUrlW(int,string,string,int,int,int);
int InternetReadFile(int,uchar  &arr[],int,int &OneInt[]);
int InternetCloseHandle(int);
#import

extern string url="https://www.mataf.net/api/tools/csv/correl/snapshot/forex/50/correlation.csv /notime";
extern string FileName="correlation.csv";

void OnTick()
  {
//---
   if(iTime(NULL,PERIOD_W1,1)!=DayBartime)
     {
      getCorrelationFile();
      DayBartime=iTime(NULL,PERIOD_W1,1);
     }
}

void getCorrelationFile()
  {
   //int HttpOpen=InternetOpenW(" ",0," "," ",0);
   //int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1);
   //int HttpRequest = InternetOpenUrlW(HttpOpen, url, NULL, 0, 0, 0);

   int read[1];
   uchar  Buffer[];
   ArrayResize(Buffer,READURL_BUFFER_SIZE+1);
   string line="";
   int fh=FileOpen(FileName,FILE_CSV|FILE_WRITE);
   if(fh>=0)
     {
      while(true)
        {
         InternetReadFile(HttpRequest,Buffer,READURL_BUFFER_SIZE,read);
         string strThisRead=CharArrayToString(Buffer,0,read[0],CP_UTF8);
         if(read[0]>0)
           {
            line = line + strThisRead;
            FileWrite(fh,line);
           }
         else
            break;
        }
      FileClose(fh);
     }
   else Alert("Error opening file ",FileName);

   if(HttpRequest>0) InternetCloseHandle(HttpRequest);
   if(HttpConnect>0) InternetCloseHandle(HttpConnect);
   if(HttpOpen>0) InternetCloseHandle(HttpOpen);
  }


 

 

I suppose it could be related to this :

Note  WinINet does not support server implementations. In addition, it should not be used from a service. For server implementations or services use Microsoft Windows HTTP Services (WinHTTP). 

 

Or you can use WebRequest() function. Regards.

https://docs.mql4.com/common/webrequest

WebRequest - Common Functions - MQL4 Reference
WebRequest - Common Functions - MQL4 Reference
  • docs.mql4.com
WebRequest - Common Functions - MQL4 Reference
 
Thanks! Let me try these out
Reason: