Download MetaTrader 5

Loading file from Internet without WebRequest()

To add comments, please log in or register
Improve your rating. Execute orders in Freelance service!
Doerk Hilger
1155
Doerk Hilger 2016.08.19 06:49 

Hey there,

WebRequest() isn't working with indicators, for whatever reason, therefore I need a possibility to load a file from a http or ftp. The samples I found to work with wininet.dll dont seem to work, any of them produces errors and not results.

I am not that familiar with internet stuff, so I am asking if there is or if someone has a simple function sample that I could use for my purpose. It the should be somehow simple like

string filecontent = ReadFromHttp("www.myurl.net/myfile.txt");

Anyone out there who could help me?

Thank you in advance

Doerk 

Stanislav Korotky
17925
Stanislav Korotky 2016.08.19 10:52  
Personally I moved to the following design: data is downloaded by WebRequest in a helper expert (by schedule or upon request) and saved into a file. All copies of indicators interested in this data, read this file.
Doerk Hilger
1155
Doerk Hilger 2016.08.19 21:08  
Stanislav Korotky:
Personally I moved to the following design: data is downloaded by WebRequest in a helper expert (by schedule or upon request) and saved into a file. All copies of indicators interested in this data, read this file.

Thanks, but that wont work in my case.

Isnt there anybody out there who knows how to make this? 

Aleksei Beliakov
24282
Aleksei Beliakov 2016.08.20 02:35  
you can make it through windows dll
Doerk Hilger
1155
Doerk Hilger 2016.08.20 09:49  

Here is the answer.

 

//+------------------------------------------------------------------+
//|                                                      wininet.mqh |
//|                                                      Dirk Hilger |
//|                  Basic code taken from "gregspinner" @ mql5.com  |
//|                                     https://www.stereotrader.net |
//+------------------------------------------------------------------+
#property copyright "Copyright (C) 2016 by Doerk (Dirk Hilger)"
#property link      "www.stereotrader.net"
#property strict
#property version   "1.00"

/*

This library contains functions for reading files from the internet
without using the original WebRequest(). 

Sample #1:

   string content=InternetGetFile("http://www.myurl.net/myfile.txt");
   
Sample #2:   
   string content=NULL;
   int errorcode=InternetGetFile("http://www.myurl.net/myfile.txt",content);
   
*/


//+------------------------------------------------------------------+
//+ Imports                                                          +
//+------------------------------------------------------------------+
#import  "Wininet.dll"
   int InternetOpenW(string, int, string, string, int);
   int InternetConnectW(int, string, int, string, string, int, int, int); 
   int HttpRequestW(int, string, string, int, string, int, string, int); 
   int InternetOpenUrlW(int, string, string, int, int, int);
   int InternetReadFile(int, uchar &sBuffer[], int, int& OneInt);
   int InternetCloseHandle(int); 
#import

#import "kernel32.dll"
   int GetLastError(void);
#import


//+------------------------------------------------------------------+
//+ Main function which returns Windows error code                   +
//+------------------------------------------------------------------+
int InternetGetFile(string url, string& content)
   {
      //--- Init
      content=NULL;
      
      //--- Create connection
      int httpconnect=0;
      int httprequest=0;
      int httpopen = Wininet::InternetOpenW("InternetGetFileMQL", 0, " "," ",0 ); 
      int e=kernel32::GetLastError();
      if (e==0)
         {
         httpconnect = Wininet::InternetConnectW(httpopen, " ", 80, "", "", 3, 0, 1); 
         e=kernel32::GetLastError();
         if (e==0)
            {
            httprequest = Wininet::InternetOpenUrlW(httpopen,url, NULL, 0, 0, 0);
            e=kernel32::GetLastError();
            if (e==0)
               {
               //--- Define buffers
               uchar ch[512];
               string temp="";
               
               //--- Retrieve data from file
               int cnt=0;
               while(Wininet::InternetReadFile(httprequest,ch,512,cnt))
                  {
                  //e=kernel32::GetLastError();
                  if(cnt<=0) break;
                  temp=temp+CharArrayToString(ch,0,cnt);
                  }
               //--- Store result
               content=temp;   
               }
            }
         }   
      
      //--- Close connection
      if (httprequest > 0) InternetCloseHandle(httprequest); 
      if (httpconnect > 0) InternetCloseHandle(httpconnect); 
      if (httpopen > 0) InternetCloseHandle(httpopen);  
      
      //--- Get out and return error code
      return(e);
   }

//+------------------------------------------------------------------+
//+ Macro function which returns content directly                    +
//+------------------------------------------------------------------+
string InternetGetFile(string url)
   {
   string content=NULL;
   int e=InternetGetFile(url,content);
   return content;
   }

To add comments, please log in or register