Download MetaTrader 5

Loading remote URL via Wininet.dll... how to detect HTTP errors ?

To add comments, please log in or register
Wemerson Guimaraes
611
Wemerson Guimaraes  

Hi.. Below is my ReadUrl() function to read remote http sites... and works fine!

But it only works with valid valid content.  If an error occurs(404, 500, etc) there is no solution so far.

So i need to detect HTTP errors. Anyone have any idea on how to do this?

#define READURL_BUFFER_SIZEX   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


void ReadUrl(string url, string& data)
{
   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_SIZEX + 1);
   data="";
   while (true)
   {
      InternetReadFile(HttpRequest, Buffer, READURL_BUFFER_SIZEX, read);
      string strThisRead = CharArrayToString(Buffer, 0, read[0], CP_UTF8);
      if (read[0] > 0)
         data = data + strThisRead;
      else
         break;
   }
   
   if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
   if (HttpConnect > 0) InternetCloseHandle(HttpConnect); 
   if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
}
Ex Ovo Omnia
3352
Ex Ovo Omnia  
Wemerson Guimaraes:

Hi.. Below is my ReadUrl() function to read remote http sites... and works fine!

But it only works with valid valid content.  If an error occurs(404, 500, etc) there is no solution so far.

So i need to detect HTTP errors. Anyone have any idea on how to do this?


I have been using HttpQueryInfoW (HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER as the info level) to get the response status code of an asynchronous request. I am not sure if it works with the above synchronous request, but you may give it a try.

Wemerson Guimaraes
611
Wemerson Guimaraes  
Ex Ovo Omnia:

I have been using HttpQueryInfoW (HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER as the info level) to get the response status code of an asynchronous request. I am not sure if it works with the above synchronous request, but you may give it a try.


Great! 

It works my friend. Thank U so much!!!

Below my Complete Wininet.mqh lib... One more time, thak you so much to help me finish my function!
I believe it can be improved and get even more improvements. But to my need it's perfect.


//+------------------------------------------------------------------+
//|                                                      Wininet.mqh |
//|                                                     Version: 1.0 |
//|                            Copyright 2015, Wemerson C. Guimaraes |
//|                  https://www.mql5.com/pt/users/wemersonrv/seller |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Wemerson C. Guimaraes"
#property link      "https://www.mql5.com/pt/users/wemersonrv/seller"
//=================================================================================================
//=================================================================================================
//====================================   ReadUrl Function   =======================================
//=================================================================================================
//=================================================================================================
#define READURL_BUFFER_SIZEX   100
#define HTTP_QUERY_STATUS_CODE  19

#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); 
   int HttpQueryInfoW(int ,int ,uchar &lpvBuffer[],int& lpdwBufferLength, int& lpdwIndex);
#import

void ReadUrl(string url, string& data)
{
        int HttpOpen = InternetOpenW(" ", 0, " ", " ", 0); 
   int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1); 
   int HttpRequest = InternetOpenUrlW(HttpOpen, url, NULL, 0, 0, 0);
   
   uchar cBuff[5];
   int cBuffLength=10;
   int cBuffIndex=0;
   int HttpQueryInfoW = HttpQueryInfoW(HttpRequest, HTTP_QUERY_STATUS_CODE, cBuff, cBuffLength, cBuffIndex);
   
   // HTTP Codes... Only the 1st character (4xx, 5xx, etc)
   int http_code = (int) CharArrayToString(cBuff, 0, WHOLE_ARRAY, CP_UTF8);   
   if( http_code==4 || http_code==5 ){ // 4XX || 5XX
      Print("HTTP Error: ", http_code, "XX");
      if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
      if (HttpConnect > 0) InternetCloseHandle(HttpConnect); 
      if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
      data="HTTP_ERROR";
   }else{
      int read[1];
      uchar  Buffer[];
      ArrayResize(Buffer, READURL_BUFFER_SIZEX + 1);
      data="";
      while (true)
      {
         InternetReadFile(HttpRequest, Buffer, READURL_BUFFER_SIZEX, read);
         string strThisRead = CharArrayToString(Buffer, 0, read[0], CP_UTF8);
         if (read[0] > 0)
            data = data + strThisRead;
         else
            break;
      }
   }
   
   if (HttpRequest > 0) InternetCloseHandle(HttpRequest); 
   if (HttpConnect > 0) InternetCloseHandle(HttpConnect); 
   if (HttpOpen > 0) InternetCloseHandle(HttpOpen);  
}
Files:
Wininet.mqh 7 kb
Laszlo Tormasi
3152
Laszlo Tormasi  
Ex Ovo Omnia:

I have been using HttpQueryInfoW (HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER as the info level) to get the response status code of an asynchronous request. I am not sure if it works with the above synchronous request, but you may give it a try.


Hello,

Is your async request function public?

To add comments, please log in or register