我正在尝试连接到 FTP 服务器以上传文件。我收到“无法连接”错误。如果我使用命令行并打开 FTP 连接,我可以连接。不知道为什么以编程方式连接时出现错误。任何帮助都将不胜感激。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://1.23.84.2");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user","password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(path);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}",
response.StatusDescription);
response.Close();