Click here to Skip to main content
15,877,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to upload a file to FTP server using SSL certificate, but receives WebException with the following message: 'The remote server returned an error: (501) Syntax error in parameters or arguments.
The code I'm using to send a file is the following:
C#
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(uri));
request.Credentials = new NetworkCredential(user, pass);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.UsePassive = false;
request.ContentLength = fileInf.Length;
//the following 3 lines causes exception on GetRequestStream()
request.EnableSsl = true;
request.ClientCertificates = new X509CertificateCollection { X509Certificate.CreateFromCertFile(@".\TestVM2.cer") };
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => { return true; };
strm = request.GetRequestStream(); // the exception occurred here

The SSL settings on the FTP server contain the corresponding certificate and SSL Policy is set to "Allow SSL connections".

Why do I get the exception and how to solve it?

What I have tried:

1. The code without the 3 problematic lines
C#
request.EnableSsl = true;
request.ClientCertificates = new X509CertificateCollection { X509Certificate.CreateFromCertFile(@".\TestVM2.cer") };
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => { return true; };
works perfectly.
Posted
Updated 26-May-22 3:31am

1 solution

Quote:
If you set UsePassive to false, then you need to make sure that the port for the command channel is open (i.e., you need to define endpoints and access rules). Unless there is a good reason to not use passive, you are far better off using passive.
From "The remote server returned an error: (501) Syntax error in parameters or arguments." when uploading TO FTP FROM Windows Azure[^]
 
Share this answer
 
Comments
Haifovchanin 26-May-22 9:53am    
Yes, I read it once, but with UsePassive set to True this does not work at all. It gives me the exception "The remote server returned an error: 227 Entering Passive Mode" with inner "SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond ..."
CHill60 26-May-22 10:05am    
Don't you think it would have been a really good idea to have included that information up front?
Looks like the FTP site is not set up to handle non-passive requests, so make sure that the port is open - you may need to speak to whoever administers that site
Haifovchanin 26-May-22 10:29am    
You are right. I'm sorry for not including this information.
Thank you for the proposed solution. I'll check it with server administrator.
Haifovchanin 29-May-22 7:40am    
I'm the person who will administer the server.
Now the state is the following:
On the server side, which is the Virtual machine on Azure:
- Firewall has the following open ports for Inbound connections: 21, 990 and 1024-65535 for TCP for both Private and Public networks.
- Firewall has the following open ports for Outbound connections: 20, 989 for TCP for both Private and Public networks.
- FTP Firewall Support is configured with the external IP address.
- FTP Site is set to use self-signed SSL Certificate and SSL Policy is set to "Allow SSL Connections.
- The SLL Certificate was exported without Private Key in Base-64 format and copied to the client's computer.
Azure Network Interface for the VM has corresponding rules for ports 21, 990, 20 and 989 (Allowed TCP connection from/to and IP).
On the client side, which is regular desktop PC:
- Firewall has the following open ports for Inbound connections: 21, 990 and 1024-65535 for TCP for both Private and Public networks.
- Firewall has the following open ports for Outbound connections: 20, 989 for TCP for both Private and Public networks.
In the C# code I still use the
UsePassive = false
line. So, As I understand, the connection type is Active, so all the traffic is via ports 21 and 20 and, for SSL case, via 989 and 990.
Still the SSL connection does not work, but the regular connection - without SSL - works fine.

What I have to do to make the SSL FTP connection work?
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900