Download MetaTrader 5

ReadUrl No Longer Working?

To add comments, please log in or register

This code used to work fine for me from March 2014 until now, then I went and compiled and tested it in a new build and this part of my code isn't working properly.

//      HTTP REQUEST          //

#define READURL_BUFFER_SIZE   1000

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

// Reads the specified URL and returns the server's response. Return value is 
// a blank string if an error occurs                
string ReadUrl(string Url, bool PrintDebuggingMessages = false)
   string strData = "";
   bool bSuccess = false;
   // Get an internet handle
   int hInternet = InternetOpenW("mt4", 0 /* 0 = INTERNET_OPEN_TYPE_PRECONFIG */, NULL, NULL, 0);
   if (hInternet == 0) {
      if (PrintDebuggingMessages) Print("InternetOpenW() failed");

   } else {
      // Get a URL handle
      int hInternetUrl = InternetOpenUrlW(hInternet, Url, NULL, 0, 0, 0);
      if (hInternetUrl == 0) {
         if (PrintDebuggingMessages) Print("InternetOpenUrlW() failed");

      } else {
         if (PrintDebuggingMessages) Print("Okay: url handle: ", hInternetUrl);

         // We potentially (in fact, usually) get the response in multiple chunks
         // Keep calling InternetReadFile() until it fails, or until
         // it says that the read is complete
         bool bKeepReading = true;
         while (bKeepReading) {                    
            int szRead[1];
            uchar arrReceive[];
            ArrayResize(arrReceive, READURL_BUFFER_SIZE + 1);
            int success = InternetReadFile(hInternetUrl, arrReceive, READURL_BUFFER_SIZE, szRead);

            if (success == 0) {
               if (PrintDebuggingMessages) Print("InternetReadFile() failed");
               bKeepReading = false;

            } else {
               // InternetReadFile() has succeeded, but we may be at the end of the data 
               if (szRead[0] == 0) {
                  if (PrintDebuggingMessages) Print("Reached end of data");
                  bKeepReading = false;
                  bSuccess = true;
               } else {
                  // Convert the data from Ansi to Unicode using the built-in MT4 function
                  string strThisRead = CharArrayToString(arrReceive, 0, szRead[0], CP_UTF8);
                  strData = StringConcatenate(strData, strThisRead);  // <-- PROBLEM HERE ON FIRST USE ONLY IN EACH MT4 SESSION

   return (strData);
//     END HTTP REQUEST    //

Now here I use ReadUrl to call a php script on my server which returns one of two things ('approved' or 'no'). It will approve only if the account number belongs to that user in the database, otherwise it returns no

ReadUrl(""+AccountNumber()+"&login="+User, true);

The problem is it will always return 'no' for some reason. I can enter the exact url it's using into my browser and I see 'approved' but mt4 is seeing 'no'. Clearly something must be messing up the url being used. I even tried swapping out AccountNumber() and User with known working combinations and still nothing.

Please help!

Carl Schreiber
Carl Schreiber  
May be you have to look at the php-script and print out what it gets - like Print(">",var,"<") to see even additional char?
To add comments, please log in or register