Downloading and Uploading File from Ftp site

 

Hi.

I want to download or upload a text file or executable file from a ftp site.

So I am using wininit.dll to do this task.

I use FtpPutFile and FtpGetFile method.

But a small bug really failing this whole codeblock. So I really wish your help to fix this code blocks.


When I run this code in my Meta Trader terminal, this is what is happening:

1) I get to initiate an internet object successfully.

2)  I am only able to get to log in of  http site in fact not ftp site using my username and password. This is good news at least but also strategy cause I can't get connect to my ftp site.

3) but once I have loged into http site instead of ftp site. I am failing to download or upload file.


Thanks in advance for your sharing your thoughts. Below is the entire code block.

Kind regards.


#define INTERNET_SERVICE_FTP    1 #define INTERNET_SERVICE_GOPHER 2 #define INTERNET_SERVICE_HTTP   3

#define FTP_TRANSFER_TYPE_UNKNOWN   0x00000000 #define FTP_TRANSFER_TYPE_ASCII     0x00000001 #define FTP_TRANSFER_TYPE_BINARY    0x00000002

#import "wininet.dll"    int InternetOpenW(string Agent, int AccessType, string ProxyName, string ProxyBypass, int Flags);    int InternetConnectW(int hInternetSession, string ServerName, int ServerPort, string UserName, string Password, int Service, int Flags, int Context);    bool FtpPutFileW(int hFtpSession, string LocalFile, string RemoteFile, int Flags, int Context);    bool FtpGetFileW(int hFtpSession, string lpszRemoteFile, string lpszNewFile, bool fFailIfExists, int dwLocalFlagsAttribute, int dwInternetFlags, int dwContext);    bool InternetCloseHandle(int hInet); #import void DownLoadUploadFile() {    int hIntObj, hIntObjConn;    string Password, ServerName, UserName;    bool success = false;       //Uploading the file    hIntObj=InternetOpenW("MyInternetObject",0, NULL,NULL,0);       printf("internet object %d", hIntObj);       if (hIntObj>0)    {          ServerName="testserver.co.uk";                   UserName="testUsername";//Your username for a ftp session          Password="testPassword";//Your password for a ftp session                   hIntObjConn=InternetConnectW(hIntObj, ServerName, 1, UserName, Password, INTERNET_SERVICE_HTTP, 0, 0);                   if (hIntObjConn>0)          {              printf("Successfully connected. %d", hIntObjConn);                           string LocalFile=TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL4\\Files\\test.dll";              string ServerFile="test.dll";                                        success=FtpGetFileW(hIntObjConn, ServerFile, LocalFile, false, 128, 2, NULL);                                        /*              string LocalFile=TerminalInfoString(TERMINAL_DATA_PATH) + "\\MQL4\\Files\\test.dll";              string HostFile="test.dll";                             success=FtpPutFileW(hIntObjConn, LocalFile, HostFile, 2, 0);              */                       }          else          {              printf("login failed. %d", hIntObjConn);          }    }          InternetCloseHandle(hIntObj);       if (success == false)    {         printf("Downloading/Uploading error. %d", success);    }    else    {         printf("Downloading/Uploading sucessfull!!");    } }

 
Young Ho Seo:

Hi.

I want to download or upload a text file or executable file from a ftp site.

So I am using wininit.dll to do this task.

I use FtpPutFile and FtpGetFile method.

But a small bug really failing this whole codeblock. So I really wish your help to fix this code blocks.


When I run this code in my Meta Trader terminal, this is what is happening:

1) I get to initiate an internet object successfully.

2)  I am only able to get to log in of  http site in fact not ftp site using my username and password. This is good news at least but also strategy cause I can't get connect to my ftp site.

3) but once I have loged into http site instead of ftp site. I am failing to download or upload file.


Thanks in advance for your sharing your thoughts. Below is the entire code block.

Kind regards.


No idea if your code is correct, but I would expect FTP values in the InternetConnect.

hIntObjConn=InternetConnectW(hIntObj, ServerName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, 0, 0); 

 
Ovo Cz:

No idea if your code is correct, but I would expect FTP values in the InternetConnect.

hIntObjConn=InternetConnectW(hIntObj, ServerName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, 0, 0); 


Yes, you are correct.


hIntObjConn=InternetConnectW(hIntObj, ServerName, INTERNET_DEFAULT_FTP_PORT, UserName, Password, INTERNET_SERVICE_FTP, 0, 0); 


I tried this code and it did not work. I have entered correct ftp user name and password too as I checked them probably more than five times.

But never had any success with Meta Trader terminal so far.


But using C sharp in another c sharp application, using WebClient method, I could login to the same ftp site. Plus I was able to download and upload the file without any problem.

So the ftp site details can't be wrong, I guess.

using (WebClient client = new WebClient())
 

Just to provide more inforamtion.

I used the same ftp site address, user name and password  on Meta Trader Terminal FTP tab.

I also got these errors. What does this mean ?

 

2016.01.21 21:54:48.311 Publisher: error connecting to ftp://testserver.co.uk
2016.01.21 22:12:38.944 FTP: cannot determine host address [ftp]
2016.01.21 22:12:32.151 Publisher: starting

 
Young Ho Seo:

Just to provide more inforamtion.

I used the same ftp site address, user name and password  on Meta Trader Terminal FTP tab.

I also got these errors. What does this mean ?

 

More interesting is that this server works for you with c#. How did you set it for the c#, as it does not seem to have any dns record.

 
Ovo Cz:

More interesting is that this server works for you with c#. How did you set it for the c#, as it does not seem to have any dns record.

Yes, C sharp, WebClient method is somehow designed for ftp file upload and download. Therefore, it did work pretty well. I guess.

But WinInet.dll seems to have little problem in initiating ftp connection inside Meta Trader Terminal.

Hope there are some MQL native ftp connection method to be developed from some knowledgable coders. :)

I am not the best anyway on ftp topics.

Kind regards.

 
Young Ho Seo:

Yes, C sharp, WebClient method is somehow designed for ftp file upload and download. Therefore, it did work pretty well. I guess.

But WinInet.dll seems to have little problem in initiating ftp connection inside Meta Trader Terminal.

Hope there are some MQL native ftp connection method to be developed from some knowledgable coders. :)

I am not the best anyway on ftp topics.

Kind regards.

 Both the WebRequest and C# WebClient very probably use the wininet.dll finally. So the question remains, how did you manage to connect to the site, which is not listed in DNS (via the C#)? Cause it makes it mysterious for me.
 
Ovo Cz:
 Both the WebRequest and C# WebClient very probably use the wininet.dll finally. So the question remains, how did you manage to connect to the site, which is not listed in DNS (via the C#)? Cause it makes it mysterious for me.

You could probably right. In fact, I am just checking that the ftp site give me error with Webclient too which did not happen before. It only happens after the New year. I guess they have changed the setting for their ftp site.

Quite possible. Thanks for letting me know any way.

Kind regards.