简体   繁体   中英

C# FTP with FtpWebRequest upload fails with “534 policy requires ssl”

When I try to upload files to an SFTP server it shows an error.

Here is my code:

string fileNameOnly = Path.GetFileName(uploadingFile);

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostAddress+ "/test1");
request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials = new NetworkCredential(UserId, Password);

StreamReader sourceStream = new StreamReader(@uploadingFile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
filesize = ConvertBytesToMegabytes(request.ContentLength);
Console.WriteLine("hello,{0}", filesize);
// Console.WriteLine("hellosss,{0}", request.ContentLength);
Stream requestStream = request.GetRequestStream(); 
Console.WriteLine("here,{0}", requestStream);

requestStream.Write(fileContents, 0, fileContents.Length);
filesizedownloaded = ConvertBytesToMegabytes(requestStream.Length);
Console.WriteLine("hellosss,{0}", filesizedownloaded);
worker.ReportProgress(Convert.ToInt32((Convert.ToInt32(filesizedownloaded) / filesize) * 100));

requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
FilesDetails.SelectedFileContent = "";
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
MessageBox.Show("File Uploaded!", " File");
response.Close();

I get an exception at Stream requestStream = request.GetRequestStream();

The error is:

Remote server returned an error (534) 534 policy requires ssl"

Your server requires an encrypted connection.

To enable the encrypted connection, set FtpWebRequest.EnableSsl :

request.EnableSsl = true;

Your code has other problems and it's unnecessarily complicated.

For a clean FTP code for C#, see Upload and download a file to/from FTP server in C#/.NET .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM