Readfile gets 5004 after webrequest

 

I have a strange problem. 

I use webrequest to pick up a file from a website. I have added the website in settings of mql4.


I find the file and I can see how big it is. But, when I try to read file, I wont get a handle! 

Now to the strange part... It works when Im running the tester!

Have I missed something easy here or what could it be?

As I said, when running tester, everything works. When running EA on chart, it cant get a handle of the file! 


This is the first time I am working with webrequest and fileopen, so I hope someone could see what I do wrong here.

   else
     {
      //--- Load successfully
      PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
      //--- Save the data to a file
      int filehandle=FileOpen(filename,FILE_CSV|FILE_READ);
      //--- Checking errors
      Print(filehandle);
      if(filehandle!=INVALID_HANDLE)
        {
            Print("File found, reading file");
            size1=22; 
            ArrayResize(ma_buff,size1); 
           // size1=(int)StringToDouble(FileReadString(handle,DOUBLE_VALUE)); 
           
             
           // ArrayResize(time_buff,size1); 
         for(int i=0;i<size1;i++) 
           { 
            //  time_buff[i]=(datetime)FileReadDouble(handle,DOUBLE_VALUE); 
               ma_buff[i]=StringToDouble(FileReadString(filehandle,DOUBLE_VALUE)); 
           } 
        resetLevels();
        setLevels();
        FileClose(filehandle);
        }
      else Print("Error in FileOpen. Error code=",GetLastError());
     }
 
PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));

You used WebRequest and received an array. That is not a file. Of course, you can't open it.

 
William Roeder #:

You used WebRequest and received an array. That is not a file. Of course, you can't open it.

Thanks for reply!


This is the hard part to not be a programmer from start (selftought) :)

I will try to figure out what you say here.


But, I think I now know why it worked in tester. I had the file in that folder since before.


Anyway, thanks for you answer. Hopefully I will learn from it :)

 
Jose Daniel Stromberg Martinez:

I have a strange problem. 

I use webrequest to pick up a file from a website. I have added the website in settings of mql4.

...

This is the first time I am working with webrequest and fileopen, so I hope someone could see what I do wrong here.

Just put the cursor on the function and press F1 then read understand and copy the example - that easy!

 

Thanks for the answers! 

I have change a lot in what I am doing. Now I write the data to a CSV file (can open the CSV and see that it is as I expect). I then open with FileOpen and read that data to different variables. When I print variables I can see that value.

But, now to the next problem. I am using this values to write out levels in EA. When I store that file local and read it from local folder, then it works like a charm. But now, values comes in to the correct variables but it wont show them in EA.

I dont know how to explain in a good way, but it is like the values doesnt is of correct type.


So, I do not get any errors, and variables do have the correct value, but its like the value doesnt mean what they say....


Hope you can help me out here.


This is to get the file and write it on disk.

void connWeb()
{
   string cookie=NULL,headers;
   string reqheaders="User-Agent: Mozilla/4.0\r\n";
   char post[],result[];
   int res;
//--- to enable access to the server, you should add URL "https://www.google.com/finance"
//--- in the list of allowed URLs (Main Menu->Tools->Options, tab "Expert Advisors"):
   string url="https://www.[someurl].com/algos/"+filename;
//--- Reset the last error code
   ResetLastError();
   Print(url);
//--- Loading a html page from Google Finance
   int timeout=5000; //--- Timeout below 1000 (1 sec.) is not enough for slow Internet connection
   res=WebRequest("GET",url,reqheaders,timeout,post,result,headers);
//--- Checking errors
   if(res==-1)
     {
      Print("Error in WebRequest. Error code  =",GetLastError());
      //--- Perhaps the URL is not listed, display a message about the necessity to add the address
      MessageBox("Add the address '"+url+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
     }
   else
     {
      //--- Load successfully
      PrintFormat("The file has been successfully loaded, File size =%d bytes.",ArraySize(result));
      //--- Save the data to a file
      int filehandle=FileOpen(filename,FILE_WRITE|FILE_BIN);
     // int filehandle2=FileOpen("test.csv",FILE_READ|FILE_CSV);
      //--- Checking errors
      Print(filehandle);
      if(filehandle!=INVALID_HANDLE)
       {
         //--- Save the contents of the result[] array to a file
         FileWriteArray(filehandle,result,0,ArraySize(result));
         //--- Close the file
         FileClose(filehandle);
         readFile();
        }
      else Print("Error in FileOpen. Error code=",GetLastError());
     }
}


This is to read the file from disk

void readFile()
  { 
   double value;
//--- open the file 
   ResetLastError(); 
   int handle=FileOpen(filename,FILE_CSV|FILE_READ); 
   if(handle<0)
      {
      Print("File NOT found, resetting levels!");
      resetLevels();
         
      }
    else
    {
      Print("File found, reading file");
      size1=22; 
      ArrayResize(ma_buff,size1); 
     
       
      for(int i=0;i<size1;i++) 
        { 
         ma_buff[i]=StringToDouble(FileReadString(handle,DOUBLE_VALUE)); 
        } 
        resetLevels();
        setLevels();
        FileClose(handle);
        if(FileDelete(filename)==true)
            Print("File deleted");
            else
               Print("could not find file");
               Print(Nb, " ", Nt, " ", ma_buff[0]);
       
    }       
      
}      
 

Problem solved! Had nothing to do with how I read the file or something like that. It was another part of EA.


For others that needs to get a file from web and read it to EA, above code is working well!


Thanks a lot!