#property copyright "Copyright © 2010, sangmane"
#property link      "sangmane@forexfactory.com"

#property show_inputs

#define INTERNET_FLAG_PRAGMA_NOCACHE    0x00000100

#import "wininet.dll"

int InternetOpenA(string sAgent, int lAccessType, string sProxyName = "", string sProxyBypass = "", int    lFlags = 0);
int InternetOpenUrlA(int hInternetSession, string sUrl, string sHeaders = "", int lHeadersLength = 0, int    lFlags = 0, int    lContext = 0);                     
int InternetReadFile(int hFile, int&   iBuffer[], int lNumBytesToRead, int&   lNumberOfBytesRead[]);                      
int InternetCloseHandle(int hInet);

extern string url = "http://content.nasdaq.com/graphs/HOMEIXIC.gif?111565";
extern string FileName = "image.gif";

int start()
{  	
	int hInternetSession = InternetOpenA("Microsoft Internet Explorer",0,"","",0);                         
  	if(hInternetSession <= 0)
  	{
    	Print("Opening Internet FAILED...");
    	return(-1);         
  	}
  	string temp;
  	int iBuffer[256];
  	int dwBytesRead[1];
  	int hURL = InternetOpenUrlA(hInternetSession, url,"",0,INTERNET_FLAG_PRAGMA_NOCACHE,0); 
  	if(hURL >0)
  	{
  		int fh = FileOpen(FileName,FILE_BIN|FILE_WRITE);
  		if(fh>=0) {
	    	while(True) {
      			if(!InternetReadFile(hURL, iBuffer, ArraySize(iBuffer)*4, dwBytesRead)) {
      				Alert("InternetReadFile Error!");
					break;
	      		}
    	  		if(dwBytesRead[0]==0) break;
				for(int i=0; i<dwBytesRead[0]; i++) {
					int byte = (iBuffer[i/4] >> ((i & 3)*8)) & 0xff;    	  		
      				FileWriteInteger(fh,byte,CHAR_VALUE);
      			}
      		}
      		FileClose(fh);
   		}
   		else Alert("Error opening file ",FileName);
    	InternetCloseHandle(hURL);  	
	}
	InternetCloseHandle(hInternetSession);
	return(0);
}	


