Download MetaTrader 5

Cant get my internet directory file download function work on 600 build...

To add comments, please log in or register
antony
549
antony  

Hi

After upgrading to 600 build I am experiencing issues with my function that otherwise work perfectly.

It returns a 1004 code and records in the log: "Error when calling InternetOpenUrlA()"

Here is the code, can anyone please help:

#define HTTP_QUERY_CONTENT_LENGTH 0x00000005
#define HTTP_QUERY_FLAG_NUMBER    0x20000000
//----
#define INTERNET_OPEN_TYPE_DIRECT       0
#define INTERNET_OPEN_TYPE_PRECONFIG    0x00000000   // use registry configuration
const uint INTERNET_FLAG_RELOAD = 0x80000000; 
#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000
#define INTERNET_FLAG_PRAGMA_NOCACHE    0x00000100
//----
#define AGENT "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)"
//+------------------------------------------------------------------+
//|     Export Function from WINAPI                                    |
//+------------------------------------------------------------------+
#import "wininet.dll"
        int InternetAttemptConnect (int x);
        int InternetOpenW                               ( string sAgent, int lAccessType, string sProxyName, string sProxyBypass, int lFlags );
        int InternetOpenUrlW                            ( int hInternetSession, string sUrl, string sHeaders, int lHeadersLength, int lFlags, int lContext );
        int InternetReadFile                            ( int hFile, int& lpvBuffer[], int lNumBytesToRead, int& lNumberOfBytesRead[] );
        int InternetCloseHandle                 ( int hInet );
        int InternetQueryDataAvailable  ( int hFile, int& lpdwNumberOfBytesAvailable[], int dwFlags, int dwContext );
        int HttpQueryInfoW                                      ( int hRequest, int dwInfoLevel, int& lpvBuffer[], int& lpdwBufferLength[], int& lpdwReserved[] );
#import
int InetToString(string fUrl, string &out){
   if(!IsDllsAllowed()){
     Alert("Must be configured to allow the use of DLL");
     return(1001);
   }
   int rv = InternetAttemptConnect(0);
   if(rv != 0){
     Print("Error when calling InternetAttemptConnect ()");
     return(1002);
   }
   int hSession = InternetOpenW(AGENT, INTERNET_OPEN_TYPE_DIRECT, "0", "0", 0);
   if(hSession <= 0){
     Print("Error when calling InternetOpenA()");
     return(1003);         
   }  
 int hReq = InternetOpenUrlW(hSession, fUrl, "0", 0,
              INTERNET_FLAG_NO_CACHE_WRITE |
              INTERNET_FLAG_PRAGMA_NOCACHE |
              INTERNET_FLAG_RELOAD, 0
              );
   if(hReq <= 0){
     Print("Error when calling InternetOpenUrlA()");
     InternetCloseHandle(hSession);
     return(1004);
   }
   int cBuffer[256];
   ArrayInitialize(cBuffer,0);
   int dwBytesRead[1]; 
   ArrayInitialize(dwBytesRead,0);
   string TXT = "";
   while(!IsStopped()){
        bool bResult = InternetReadFile(hReq, cBuffer, 1024, dwBytesRead);
        if(dwBytesRead[0] == 0)break;
        string text = "";   
        for(int i = 0; i < 256; i++){
            text = text + CharToStr(cBuffer[i] & 0x000000FF);
            if(StringLen(text) == dwBytesRead[0])break;
            text = text + CharToStr(cBuffer[i] >> 8 & 0x000000FF);
            if(StringLen(text) == dwBytesRead[0])break;
            text = text + CharToStr(cBuffer[i] >> 16 & 0x000000FF);
            if(StringLen(text) == dwBytesRead[0])break;
            text = text + CharToStr(cBuffer[i] >> 24 & 0x000000FF);
            if(StringLen(text) == dwBytesRead[0])break;
          }
        TXT = TXT + text;
        Sleep(1);
      }
   if(StringFind(TXT,"<html>",0)>=0||StringFind(TXT,"<title>",0)>=0||StringFind(TXT,"<head>",0)>=0){
    if(StringFind(TXT,">404",0)>=0){return(404);}
   }  
   out = TXT;  
   InternetCloseHandle(hReq);
   InternetCloseHandle(hSession);              
   return(0);         
}
antony
549
antony  

Thanks in advance


Antony

Carl Schreiber
Moderator
7944
Carl Schreiber  

try this:

#import "wininet.dll"
   int InternetOpenW(string,int,string,string,int);
   int InternetConnectW(int,string,int,string,string,int,int,int);
   int InternetOpenUrlW(int,string,string,int,int,int);
   int InternetReadFile(int hFile,uchar &sBuffer[],int lNumBytesToRead,int &lNumberOfBytesRead[]);   
   int InternetCloseHandle(int);
#import

string GrabWeb(string strUrl) {//>4
   string result;
   int HttpOpen=InternetOpenW(" ",0," "," ",0);
   int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1);
   int HttpRequest = InternetOpenUrlW(HttpOpen,strUrl, NULL, 0, 0, 0);
        
   int read[1];
   int sz=0;   
   uchar buffer[1024];
   bool isOk;
   while(true) {
      isOk = InternetReadFile(HttpRequest,buffer,1024,read);
      sz += read[0];
      if(read[0]>0) result=result+CharArrayToString(buffer,0,read[0],CP_UTF8);
      else break;
   }

   if(HttpRequest>0) InternetCloseHandle(HttpRequest);
   if(HttpConnect>0) InternetCloseHandle(HttpConnect);
   if(HttpOpen>0) InternetCloseHandle(HttpOpen);

   if (sz>0) return (result);
   // else
   return("");
}

( I changed my code and didn't compile it..!)

antony
549
antony  

Works perfectly, thank you so much.


Antony

antony
549
antony  

Just spotted a small issue, when the file is deleted from the server, the function still returns the file contents, is this data being held in a buffer or cache?

Perhaps these will need to be flushed at the end of the function?


Thanks

Antony

Carl Schreiber
Moderator
7944
Carl Schreiber  

I have no idea.

And I think even if I would know more about the Server and the files loaded I wouldn't know.

Could it be a local (your pc) Internet buffer or could it be buffered by your internet provider?

antony
549
antony  

On the old script it uses this:

Wonder if this can be used in the GrabWeb() function?

int hReq = InternetOpenUrlW(hSession, fUrl, "0", 0,
              INTERNET_FLAG_NO_CACHE_WRITE |
              INTERNET_FLAG_PRAGMA_NOCACHE |
              INTERNET_FLAG_RELOAD, 0
              );
#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000
#define INTERNET_FLAG_PRAGMA_NOCACHE    0x00000100
antony
549
antony  

Thanks again

Antony

qjol
3245
qjol  

tonyjms2005:

Wonder if this can be used in the GrabWeb() function?


yes
antony
549
antony  

Ah right cool, like this:

#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000

string GrabWeb(string strUrl) {//>4
   string result;
   int HttpOpen=InternetOpenW(" ",0," ",INTERNET_FLAG_NO_CACHE_WRITE,0);
   int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1);
   int HttpRequest = InternetOpenUrlW(HttpOpen,strUrl, NULL, 0, 0, 0);
        
   int read[1];
   int sz=0;   
   uchar buffer[1024];
   bool isOk;
   while(true) {
      isOk = InternetReadFile(HttpRequest,buffer,1024,read);
      sz += read[0];
      if(read[0]>0) result=CharArrayToString(buffer,0,read[0],CP_UTF8);
      else break;
   }

   //if(HttpRequest>0) 
   InternetCloseHandle(HttpRequest);
  // if(HttpConnect>0) 
   InternetCloseHandle(HttpConnect);
 //  if(HttpOpen>0) 
   InternetCloseHandle(HttpOpen);

   if (sz>0) Print("res",result);return (result);
   // else
   return("");
}
Carl Schreiber
Moderator
7944
Carl Schreiber  
string GrabWeb2(string strUrl) {//>4

   int HttpOpen=InternetOpenW(" ",0," "," ",0);
   int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1 );
   //int HttpRequest = InternetOpenUrlW(HttpOpen,strUrl,    NULL, 0, 0, 0 );
   int HttpRequest = InternetOpenUrlW(HttpOpen, strUrl, "0", 0, INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE, 0 );
   
   string result:
   int read[1];
   int sz=0;   
   uchar buffer[1024];
   bool isOk;
   while(true) {
      isOk = InternetReadFile(HttpRequest,buffer,1024,read);
      sz += read[0];
      if(read[0]>0) result=result+CharArrayToString(buffer,0,read[0],CP_UTF8);
      else break;
   }

   /*if(HttpRequest>0)*/  InternetCloseHandle(HttpRequest);
   /*if(HttpConnect>0)*/  InternetCloseHandle(HttpConnect);
   /*if(HttpOpen>0)*/     InternetCloseHandle(HttpOpen);
   if (sz>0) { Print("res: ",result); return(result); }
   return("");
}

This I could compile without error, but haven't checked it working.

Can you tell me whether you still have the chache-problem?

12
To add comments, please log in or register