Download MetaTrader 5
To add comments, please log in or register
Interested in MQL5 features? Find them in documentation!
Alfred
15
Alfred 2014.02.24 18:56 

I tried to search for a similar problem, but couldn't find it.

I use a snippet to read an URL from MetaTrader, found here: https://forum.mql4.com/60608/page2 This works when I put the code in the OnTimer (or OnInit) function, but not when I put it in a own function which I call from the OnTimer function.

This works and shows the value of the text file on my server:

#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, string, int, int& OneInt[]);
   int InternetCloseHandle(int); 
#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
   EventSetTimer(5);
      
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
   string URL = "http://www.forexfactory.com/ff_calendar_thisweek.xml";
   
   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];
   string Buffer = " ";
   string page = "";
   
   while (true)
   {
      InternetReadFile(HttpRequest, Buffer, StringLen(Buffer), read);
      if (read[0] > 0)
                        page = page + StringSubstr(Buffer, 0, read[0]);
      else              break;
   }
   
   if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
   if (HttpConnect > 0) InternetCloseHandle(HttpConnect); 
   if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
   MessageBox(page, "HTTP READ:" ); 
}

This doesn't work:

#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, string, int, int& OneInt[]);
   int InternetCloseHandle(int); 
#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
   EventSetTimer(5);
      
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
   MessageBox(geturl("http://www.forexfactory.com/ff_calendar_thisweek.xml"), "Read response");
}
//+------------------------------------------------------------------+
string geturl(string url)
{   
   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];
   string Buffer = " ";
   string page = "";
   
   while (true)
   {
      InternetReadFile(HttpRequest, Buffer, StringLen(Buffer), read);
      if (read[0] > 0)
                        page = page + StringSubstr(Buffer, 0, read[0]);
      else              break;
   }
   
   if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
   if (HttpConnect > 0) InternetCloseHandle(HttpConnect); 
   if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
   MessageBox(page, "HTTP READ:" );    
   
   return page;
}

The messagebox inside the geturl function is blank, same as the messagebox in the timer function. The URL given in the Timer function is correct when I print it in the geturl function. So something goes wrong on the call to the website itself, but I can't figure out what. Any ideas?

Edit: 20140226: replaced "mydummywebsite.com" with "http://www.forexfactory.com/ff_calendar_thisweek.xml"

James Hodges
2802
James Hodges 2014.02.24 20:11  

Just looking at it quickly but it looks like your function pops up the MessageBox for you so you don't need to call it from INSIDE a MessageBox in the Ontimer() function.. looks like a simple

geturl("http://www.mydummywebsite.com/test.txt");

Might work.
But like I said I didn't really study it... good luck.

PipPip..jimdandy

Alfred
15
Alfred 2014.02.25 08:57  

Hi Jimdandy

Thanks for the reply. I tried the MessageBox inside both functions for some sort of 'debugging', to be sure it wasn't going wrong with the function return. But both MessageBoxes pop up blank in my second example.

Alfred
15
Alfred 2014.02.26 08:45  
No one else an idea what is going wrong in my code?
qjol
3243
qjol 2014.02.26 09:08  

i realy dont know what u want it's not a real address

i checked the address (http://www.mydummywebsite.com/test.txt) manually & i get 404 page not found

Alfred
15
Alfred 2014.02.26 09:52  

The original URL was replaced, I thought "mydummywebsite.com" would make that clear.

I'll change the post with a working URL, like for example: http://www.forexfactory.com/ff_calendar_thisweek.xml

qjol
3243
qjol 2014.02.26 10:35  
#define READURL_BUFFER_SIZE   100

#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

geturl("http://www.forexfactory.com/ff_calendar_thisweek.xml");

string geturl(string url)
   {   
   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 page = "";
   while (true)
      {
      InternetReadFile(HttpRequest, Buffer, READURL_BUFFER_SIZE, read);
      string strThisRead = CharArrayToString(Buffer, 0, read[0], CP_UTF8);
      if (read[0] > 0)
         page = page + strThisRead;
      else
      break;
      }
   
   if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
   if (HttpConnect > 0) InternetCloseHandle(HttpConnect); 
   if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
   MessageBox(page, "HTTP READ:" );    
   
   return page;
}

Alfred
15
Alfred 2014.02.26 11:19  
Thank you qjol, that works!
/
To add comments, please log in or register