1

I want to upload and download a file to FTP over ssl with custom port number or FTP with implicit SSL. I am using .net ftpwebrequest & ftpwebresponse method for this. here is my code:

Dim reqObj As FtpWebRequest = CType(WebRequest.Create(_Url + filename), FtpWebRequest) 
reqObj.Method = WebRequestMethods.Ftp.UploadFile 
reqObj.KeepAlive = False 
If _IsSSLEnable Then 
reqObj.UseBinary = True 
End If 
reqObj.Credentials = New NetworkCredential(_UserName, _Password) 
If _IsSSLEnable Then 
reqObj.EnableSsl = _IsSSLEnable Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf validateCert  
End If 
Dim streamResponse As Stream 
streamResponse = reqObj.GetRequestStream() 

I am getting error after this...

The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.

But if i Add

reqObj.UsePassive = False

It gives me error:

The operation has timed out

this is working fine for normal ftp and ftp over ssl but not for custom port number or implicit ssl.

Can you please tell me does .net support this?

Or I have to use other method for this case

Thanks,

Hemant

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
hemant
  • 21
  • 1
  • 4
  • Hemant please edit your question showing your code and the error details. No Crystal Balls here ;-) – Davide Piras Sep 21 '11 at 12:09
  • @Davide Thanks for your reply.here is my code ::: Dim reqObj As FtpWebRequest = CType(WebRequest.Create(_Url + filename), FtpWebRequest) reqObj.Method = WebRequestMethods.Ftp.UploadFile reqObj.KeepAlive = False If _IsSSLEnable Then reqObj.UseBinary = True End If reqObj.Credentials = New NetworkCredential(_UserName, _Password) If _IsSSLEnable Then reqObj.EnableSsl = _IsSSLEnable Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf validateCert 'callback End If Dim streamResponse As Stream streamResponse = reqObj.GetRequestStream() – hemant Sep 21 '11 at 14:44
  • edit the question putting the code inside the question and format is nicely as code please – Davide Piras Sep 21 '11 at 15:05
  • 1
    A quick google search says No. The FtpWebRequest does not support Implicit SSL. As for the port, we use 21, so I set it to 21 when building the URI (using UriBuilder) to create the FtpWebRequest. http://stackoverflow.com/questions/1842186/does-net-ftpwebrequest-support-both-implicit-ftps-and-explicit-ftpes – MikeTeeVee Oct 11 '12 at 22:37

1 Answers1

1

there is a very similar question already on SO: Set Port number when using FtpWebRequest in C#

it looks like the solution was this:

yourFtpWebRequest.UsePassive = false;

but check that question and answer for more details.

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Thanks for your reply. I have done that changes but my proble not resolved. In my case there is difference that when i tried to connect via FileZilla it connect to a specific port not to port 21. Thanks, Hemant – hemant Sep 21 '11 at 10:35
  • Also for one Ftp over ssl it gives me this error. In this there is no specific port . The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made. – hemant Sep 21 '11 at 10:43
  • how are you creating the request? you should do something like this: FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftps://[host]:[PortNumber]/[filename.ext]"); ... – Davide Piras Sep 21 '11 at 11:16
  • Dim request As FtpWebRequest = CType(WebRequest.Create("ftps://[Host}:[port]/" + filename), FtpWebRequest) it gives me error:The URI prefix is not recognized. but if I try with Dim request As FtpWebRequest = CType(WebRequest.Create("ftp://[Host}:[port]/" + filename), FtpWebRequest) means without ftps it gives me error: The underlying connection was closed: The server committed a protocol violation – hemant Sep 21 '11 at 12:07
  • Thank you SO Much! It fixed my prob. This will depend on the firewall between the Machines. I've noticed if I go through our firewall I need it left at true, otherwise it will return the Exception: "The remote server returned an error: (500) Syntax error, command unrecognized." However, if I'm behind the firewall (like two machines connecting directly to each other within a data-center) then I need to set it to false, otherwise it will return the Exception: "The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made." – MikeTeeVee Oct 11 '12 at 22:40