Scripts: WININET_TEST

 

WININET_TEST:

Here is a simple example that shows how to download page(file) from Internet using the wininet.dll library.

The WININET_TEST.mq5 script is a modified version of script s_wininet.mq4, published by Integer in MQL4.Codebase.

To see how does it works, don't forget to "Allow DLL imports" in the options of the client terminal and execute it at any chart.


Author: MetaQuotes

 

Can you tell me if it is possible to know the length of a file before it is read? This is to be able to make an estimate of the percentage of loaded data in case of large volumes, slow speeds.

 
gdtt:

Can you tell me if it is possible to know the length of a file before it is read? This is to be able to estimate the percentage of data downloaded in case of large volumes, slow speeds.

I think you can. and you will find it in the msdn description of wininet.dll.

 
gdtt:

Can you tell me if it is possible to know the length of a file before it is read? This is to be able to make an estimate of the percentage of loaded data in case of large volumes, slow speeds.

Send /HEAD before /GET request
 
sergeev:

I think you can. and you will find it in the msdn description of wininet.dll.


I suppose it would be a good topic for an article with full disclosure of wininet.dll usage

 
I'm all for it! It'll be a very useful article.
 
gdtt:

Can you tell me if it is possible to know the length of a file before it is read? This is to be able to make an estimate of the percentage of downloaded data in case of large volumes, slow speeds.

There is a possibility if the server reports the length of the content.

To get information about the request you can use the HttpQueryInfo function from wininet.dll - this function returns the required information to the specified array in string form.

Example of use:

in the #import section, add a description:

int HttpQueryInfoW(int hRequest,int dwInfoLevel,uchar &lpvBuffer[],int &lpdwBufferLength,int &lpdwIndex);

also add

#define HTTP_QUERY_CONTENT_LENGTH 5

then in the code after calling InternetOpenUrlW and getting hURL you need to add the code:

   int BufLen=2048;
   int ind=0;
   uchar buf0[2048];
   string s="";
   int ContentSize_HttpQueryInfoW=0;
   int iRes;
   
   iRes=HttpQueryInfoW(hURL,HTTP_QUERY_CONTENT_LENGTH,buf0,BufLen,ind);
   if(iRes<=0)
     {
      Print("Error in call of HttpQueryInfoW()");
     }
   else
     {
      s="";
      for(int k=0;k<BufLen;k++) { s=s+CharToString(buf0[k]);}
      Print("HTTP_QUERY_CONTENT_LENGTH:",s);
      if (StringLen(s)>0) ContentSize_HttpQueryInfoW=StringToInteger(s);
      Alert("Content size=",ContentSize_HttpQueryInfoW);
     }     

in our test example from yandex.ru we get "Content size= 44649", which corresponds to the size of the downloaded file. i.e. before the start of the download (InternetReadFile) we know the length of the content.

 

Added this feature to the current version of the script.

 

Thank you for the effort!

It works perfectly! 

 

When I try to run it the metatrader closes with a fatal error. Any solution?

 
I found that wininet worked before the last MT5 update. But I still cant find a solution to make it work again. please let me know if you find how to overcome this problem.