无法使用.net FTPCLIENT对象从Windows服务器连接FTP

时间:2019-02-06 13:46:22

标签: asp.net .net amazon-ec2 ftp ftp-client

我们正在尝试连接到托管在AWS EC2实例上的FTP服务器。这是我们用来将文件上传到FTP服务器的代码。

string ftp =“ ftp://server.com/”;

//FTP Folder name. Leave blank if you want to upload to root folder.
string ftpFolder = "/";

byte[] fileBytes = null;

//Read the FileName and convert it to Byte array.
string fileName = Path.GetFileName(FileUpload1.FileName);
using (StreamReader fileStream = new StreamReader(FileUpload1.PostedFile.InputStream))
{
    fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
    fileStream.Close();
}

try
{
    //Create FTP Request.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    //Enter FTP Server credentials.
    request.Credentials = new NetworkCredential("username", @"password");
    request.ContentLength = fileBytes.Length;
    request.UsePassive = true;
    request.UseBinary = true;
    request.ServicePoint.ConnectionLimit = fileBytes.Length;
    request.EnableSsl = false;

    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(fileBytes, 0, fileBytes.Length);
        requestStream.Close();
    }

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    lblMessage.Text += fileName + " uploaded.<br />";
    response.Close();
}
catch (WebException ex)
{
    throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}

该代码可在我们的本地计算机上运行。但是在Windows生产服务器上却无法使用。不过,相同的FTP可以与Windows生产服务器中的filezilla一起使用。

我们已经在AWS入站规则中将Windows生产服务器ip列入了白名单。

请帮助我们找出解决方案。

0 个答案:

没有答案