Indicator Error in WebRequest. Error code =4060

 
Hello, I'm trying to add a WebRequest() to my indicator but keep getting 'Error in WebRequest. Error code  =4060'. I have already added the URL https://google.com to Tools > Options > Expert Advisors. Does WebRequest() work only with expert advisors? Thanks!

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   string cookie=NULL,headers;
   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 google_url="https://www.google.com/finance";
//--- Reset the last error code
   ResetLastError();
//--- 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",google_url,cookie,NULL,timeout,post,0,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 '"+google_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("GoogleFinance.htm",FILE_WRITE|FILE_BIN);
      //--- Checking errors
      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);
        }
      else Print("Error in FileOpen. Error code=",GetLastError());
     }
  }
 
Have you entered the url you want get in the terminal? Ctrl-O => Tab Expert => Allow following URLs
 
dwas:
Hello, I'm trying to add a WebRequest() to my indicator but keep getting 'Error in WebRequest. Error code  =4060'. I have already added the URL https://google.com to Tools > Options > Expert Advisors. Does WebRequest() work only with expert advisors? Thanks!

You can not use WebRequest() from an indicator
 
Carl Schreiber:
Have you entered the url you want get in the terminal? Ctrl-O => Tab Expert => Allow following URLs
Yes
Mladen Rakic:
You can not use WebRequest() from an indicator
Thank you! Too bad
 

WebRequest() does not work with indicators, but MessageBox() also not.

You could simply use the functions from "wininet.dll" (InternetReadFile()) directly instead of using WebRequest(). BUT - you will definitely crash MT4 sooner or later when you try to use this within OnCalculate() along with some file writing actions. If you want this to work proper, you should deal with asynchronous processing and the calling of internet functions during OnTimer().

Anyway, this is a part of function set I wrote and it works well. Just call

string content = InternetGetHTTPContent("https://www.google.com/finance");


It will work with MT4 and MT5.

Enjoy

//+------------------------------------------------------------------+
//|                                                      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"

#define INTERNET_DEFAULT_FTP_PORT 21
#define INTERNET_SERVICE_FTP 1            
#define INTERNET_FLAG_PASSIVE 0x8000000   
#define INTERNET_SERVICE_HTTP 3
#define INTERNET_DEFAULT_HTTP_PORT 21
#define INTERNET_OPEN_TYPE_PRECONFIG 0

#define FTP_TRANSFER_TYPE_UNKNOWN 0x0   
#define FTP_TRANSFER_TYPE_ASCII 0x1   
#define FTP_TRANSFER_TYPE_BINARY 0x2  
#define INTERNET_FLAG_TRANSFER_UNKNOWN 0x0
#define INTERNET_FLAG_TRANSFER_ASCII 0x1
#define INTERNET_FLAG_TRANSFER_BINARY 0x2

#define GENERIC_WRITE 0x40000000
#define GENERIC_READ 0x80000000
/*

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);

enum ENUM_SSL_USAGE
   {
      SSL_DEFAULT = 0,
      SSL_ON = 1,
      SSL_OFF = 2
   };

//#include <StereoTrader_API\ArgStr.mqh>
//+------------------------------------------------------------------+
//+ Imports                                                          +
//+------------------------------------------------------------------+
#define LPCTSTR string
#define HINTERNET int 
#import  "Wininet.dll"
   HINTERNET InternetOpenW(string name, int config, string, string, int);
   HINTERNET InternetConnectW(HINTERNET hopen, string, int, string, string, int, int, int); 
   HINTERNET HttpRequestW(HINTERNET hconnect, string, string, int, string, int, string, int); 
   HINTERNET InternetOpenUrlW(int, string, string, int, int, int);
   bool InternetReadFile(int, uchar &sBuffer[], int, int& OneInt);
   bool InternetCloseHandle(int); 
   bool FtpPutFileW(HINTERNET hconnect, string localfile, string remotefile, int flags, int flags);
   bool FtpCreateDirectoryW(HINTERNET hconnect, string name);
   bool FtpDeleteFileW(HINTERNET hconnect, string name);
   HINTERNET FtpOpenFileW(HINTERNET hconnect, string filename, int access, int flags, int context);
   bool InternetWriteFile(HINTERNET hfile, uchar &buffer[], int bytes, int &byteswritten);
#import

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

//--------------------------------------------------
// Custom ID
//--------------------------------------------------
string __wininet_connect ="StereoTrader_API";
bool   __wininet_ssl = true;
void  InternetSetIDString(string id)
   {
      __wininet_connect="StereoTrader_API";
      if (id==NULL)  
         return;
      __wininet_connect+="_"+id;
   }
//--------------------------------------------------
// SSL workaround
//--------------------------------------------------
string CheckHTTP(string url, ENUM_SSL_USAGE sslmode)
   {
      if (StringFind(url,"http")<0)
         url="http://"+url;
      bool ssluse=__wininet_ssl;
      if (sslmode==SSL_OFF)   
         ssluse=false;
      else if (sslmode==SSL_ON)
         ssluse=true;
      if (ssluse)
         StringReplace(url,"http://","https://");
      else
         StringReplace(url,"https://","http://");
      return url;
   }
//+------------------------------------------------------------------+
//+ Main function which returns Windows error code                   +
//+------------------------------------------------------------------+
int InternetGetHTTPContent(string url, string& content, ENUM_SSL_USAGE sslmode=SSL_DEFAULT)
   {
      //--- Init
      content=NULL;
      
      //--- Validate URL
      url=CheckHTTP(url,sslmode);
      
      //--- Create connection
      int httpconnect=0;
      int httprequest=0;
      int httpopen = Wininet::InternetOpenW(__wininet_connect, 0, " "," ",0 ); 
      int e=kernel32::GetLastError();
      if (e==0)
         {
         httpconnect = Wininet::InternetConnectW(httpopen, " ", INTERNET_DEFAULT_HTTP_PORT, "", "", INTERNET_SERVICE_HTTP, 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]={0,0,0,0};
               string temp="";
               
               //--- Retrieve data from file
               int cnt=0;
               while(Wininet::InternetReadFile(httprequest,ch,512,cnt))
                  {
                  if(cnt<=0) break;
                  #ifdef __MQL4__
                  temp=temp+CharArrayToString(ch,0,cnt);
                  #else
                  if (cnt>1 && ch[1]==0)
                     {
                     for (int i=0;i<cnt;i++)
                        temp=temp+CharArrayToString(ch,i*2,1);
                     }
                  else
                     temp=temp+CharArrayToString(ch,0,cnt);
                  #endif
                  
                  }
               //--- 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 InternetGetHTTPContent(string url, bool autodisablecache=true, ENUM_SSL_USAGE sslmode=SSL_DEFAULT)
   {
      string content=NULL;
      //--- Add random GET parameter to URL to avoid caching
      if (autodisablecache)
         {
         double rnd=::GetTickCount();
         string rndstr=DoubleToString(rnd);
         if (StringFind(url,"?")>=0)
            url=url+"&a="+rndstr;
         else
            url=url+"?a="+rndstr;
         }
      int e=InternetGetHTTPContent(url,content,sslmode);
      return content;
   }

 
Doerk Hilger:

WebRequest() does not work with indicators, but MessageBox() also not.

You could simply use the functions from "wininet.dll" (InternetReadFile()) directly instead of using WebRequest(). BUT - you will definitely crash MT4 sooner or later when you try to use this within OnCalculate() along with some file writing actions. If you want this to work proper, you should deal with asynchronous processing and the calling of internet functions during OnTimer().

Anyway, this is a part of function set I wrote and it works well. Just call

string content = InternetGetHTTPContent("https://www.google.com/finance");


It will work with MT4 and MT5.

Enjoy


Hi,

I able to OpenUrlW but i got error message {"error":"unsupported_grant_type"}

how can i send the data below to get back a correct message?

Thanks.


URL: http://47.91.231.122:5002/token

username:admin@admin.com

password:111111

grant_type:password

my code:

int init() {

      string headers = "Content-Type: application/x-www-form-urlencoded\r\n";

      string data = "{\"username\":\"admin@admin.com\",\"password\":\"111111\",\"grant_type\":\"password\"}";

      string acceptTypes[1] = {"*/*"};

      int httpconnect=0;

      int httprequest=0;

      

      int httpopen = Wininet::InternetOpenW("Phoenix_API", 0, " "," ",0 ); 

      int e=kernel32::GetLastError();

      printf("HttpOpen=0 = "+httpopen+", "+e); 

      if (e==0)

         {

         httpconnect = Wininet::InternetConnectW(httpopen, "47.91.231.122", 5002, NULL, NULL, 3, 0, 1);

         printf("httpconnect=0 = "+httpconnect+", "+e); 

         e=kernel32::GetLastError();

         if (e==0)

            {

            httprequest = Wininet::InternetOpenUrlW(httpopen,"http://47.91.231.122:5002/token", headers, StringLen(headers), 0, 0);

            e=kernel32::GetLastError();

            printf("httprequest=0 = "+httprequest+", "+e); 

            if (e==0)

               {             

               int HttpRequest = HttpOpenRequestA(httpconnect, "POST", NULL, NULL, NULL, acceptTypes, 0, 1);

               e=kernel32::GetLastError();

               printf("HttpRequest = "+HttpRequest+", "+e);

               if (e==0)

                  {    

                  bool A = HttpSendRequestA(HttpRequest, headers, StringLen(headers), data, StringLen(data));

                  e=kernel32::GetLastError();

                  printf("A = "+A+", "+e);  

     

                  //--- Define buffers

                  uchar ch[512]={0,0,0,0};

                  string temp="";

                  

                  //--- Retrieve data from file

                  int cnt=0;

                  while(Wininet::InternetReadFile(httprequest,ch,512,cnt))

                     {

                     if(cnt<=0) break;

                     temp=temp+CharArrayToString(ch,0,cnt);

                     printf(temp);

                    } 

                  MessageBox(temp, "HTTP READ:", 0x00000030);                

                  }

               }

            }

         }   

      

      //--- Close connection

      if (httprequest > 0) InternetCloseHandle(httprequest); 

      if (httpconnect > 0) InternetCloseHandle(httpconnect); 

      if (httpopen > 0) InternetCloseHandle(httpopen);  

}


Reason: