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

 

With that code the function doesn't seem to obtain any file info, it just Prints(); 0


Thanks


Antony

 

hmm,

the mt4-compiler throws an error if you do:

int x = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD;

... ';' - unexpected token      iNet.mqh        382     129

so MT4 does not understand more than 2 '|' (Bitwise OR) :(

This compiles well - can you try:

//change the import of InternetOpenUrlW:
   int InternetOpenUrlW(int,string,string,int,uint,int);

...
// and in GrabWeb():

   uint x = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE;
   x = x | INTERNET_FLAG_RELOAD;
   int HttpRequest = InternetOpenUrlW(HttpOpen, strUrl, "0", 0, x, 0 );

or the whole function:

string GrabWeb2(string strUrl) {

   int HttpOpen=InternetOpenW(" ",0," "," ",0);
   int HttpConnect = InternetConnectW(HttpOpen, "", 80, "", "", 3, 0, 1 );
   //int HttpRequest = InternetOpenUrlW(HttpOpen,strUrl,    NULL, 0, 0, 0 );
   
   uint x = INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE;
   x = x | INTERNET_FLAG_RELOAD;
   int HttpRequest = InternetOpenUrlW(HttpOpen, strUrl, "0", 0, x, 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("");
}

at the compiler doesn't cry, but can you load you files?

 

Hi

Sorry for the late reply, that version doesn't load the file data :(

Antony

 

Does anyone know why the original version of the function has ceased to work with build 600? :


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

Its basically returning 1004

Thanks

Antony

 
I set the A's to W's on the functions but that hasn't helped, any ideas?
 

Maybe the flags are getting messed up. You are defining them as uint but passing them to the function as int.

This is how I do it.

//web fetch test script
#property strict
#property show_inputs
input string address="http://www.forexfactory.com/ff_calendar_thisweek.xml";
void start() {MessageBox(StringSubstr(GetURL(address),0,1000), "Web Fetch:");}

#import "wininet.dll"
int InternetOpenW(string sAgent, int lAccessType=0, string sProxyName="", string sProxyBypass="", uint lFlags=0);
int InternetOpenUrlW(int hInternetSession, string sUrl, string sHeaders="", int lHeadersLength=0, uint lFlags=0, int lContext=0);
int InternetReadFile(int hFile, uchar& sBuffer[], int lNumBytesToRead, int& lNumberOfBytesRead[]);
int InternetCloseHandle(int hInet);
#import

#define INTERNET_FLAG_PRAGMA_NOCACHE    0x00000100 // Tell proxy not to read cache
#define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000 // Don't write cache
#define INTERNET_FLAG_RELOAD            0x80000000 // Don't read cache
#define INTERNET_AGENT                  "Mozilla/4.0 (compatible; MT4-News/1.0;)"
#define INTERNET_READ_BUFFER_SIZE       4096

string GetURL(string url)
  {
  uint flags=INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD;
  int inetsesshandle = InternetOpenW(INTERNET_AGENT);
  if (inetsesshandle==0) return("Error: InternetOpen");
  int ineturlhandle = InternetOpenUrlW(inetsesshandle, url, NULL, 0, flags);
  if (ineturlhandle==0) {InternetCloseHandle(inetsesshandle); return("Error: InternetOpenUrl");}
  int lreturn[1], ineterr=0;
  uchar buffer[INTERNET_READ_BUFFER_SIZE];
  string content="";
  while (!IsStopped())
    {
    if (InternetReadFile(ineturlhandle, buffer, INTERNET_READ_BUFFER_SIZE, lreturn)==0)
      {content="Error: InternetReadFile"; break;}
    if (lreturn[0]<=0) break;
    content = content + CharArrayToString(buffer, 0, lreturn[0], CP_ACP);
    }
  InternetCloseHandle(ineturlhandle);
  InternetCloseHandle(inetsesshandle);
  return(content);
  }
 

Hi

Thank you eiclid this works an absolute treat, I will fully test the whole scipt when the markets re-open.

Thank you once again

Antony

Reason: