How to open an URL and download a file from an URL?

 

Hi everyone,

I searched in forum but cannot find how to open an URL in MT4 and download a file from an URL. What I simply need is 2 scripts:

- One script that open an URL (string type) that given in input of the script.

- One script that download a file from an URL, such as: http://www.domain.com/folder/file.ext (whatever the file type is).

Can anyone help me? Thanks a lot.

 
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#import "shell32.dll"
   int ShellExecuteW(int hWnd, string Verb, string File, string Parameter, string Path, int ShowCommand);
#import
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
  ShellExecuteW(0, "Open","http://bit.ly/1gcKBFL", 0 , "", 0);
  }
//---
 

Thank you so much, gjol.

This is quite helpful.

By the way, how to use all function of DLL?

There are some DLL such as "shell32.dll", "wininet.dll", .etc... But how can I know all of their function as well as parameter's meaning?

Is there any document to instruct/guide about this (like mql4)?

Thanks again.

 
Kienvu:

Hi everyone,

I searched in forum but cannot find how to open an URL in MT4. What I simply need is a script that open an URL (string type) that given in input of the script.

Can anyone help me? Thanks a lot.

This might be helpful for you.

HTTP requests for new MQL4 (tested in build 600)


https://www.mql5.com/en/code/11119

- CodeMonkey

 
Kienvu:

There are some DLL such as "shell32.dll", "wininet.dll", .etc... But how can I know all of their function as well as parameter's meaning?

google is your friend

Is there any document to instruct/guide about this (like mql4)?

of course MSDN (no it's not like mql4)

 

I have tried this code from a post in forum, but when I run it nothing happened (I tried to download an image from an website). Can anyone know why?

#property copyright "Integer"
#property link "for-good-letters@yandex.ru"

#import "wininet.dll"
#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://www...../......./abc.jpg";
extern string FileName = "abc.jpg";

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

i assume you using B600 >...

you have to adapt your code for UNICODE so you have to use for instance InternetOpenW instead of InternetOpenA see an example here



 
Great thanks, gjol. It worked!
 
qjol:
Where to paste this code? please help.
Reason: