FTPWebRequest:无法连接到服务器

时间:2018-08-05 09:51:52

标签: c# asp.net iis ftpwebrequest

我已经在ASP.NET中编写了一个站点,该站点每15秒使用FTPWebRequest从FTP服务器下载一个文本文件。当我在计算机上运行它时,它就可以正常工作。当我将其上传到我们的服务器时,FTP下载失败。也不例外,它只会返回0个值。

我认为防火墙可能有问题,因此我禁用了Windows防火墙,并禁用了传出防火墙,这是同样的问题。尝试了主动和被动FTP。我是否需要在IIS管理器中更改某些设置?

在与Exchange 2016相同的VM上的Server 2012 R2上运行IIS 10。

部分代码:

public static string[] GetTXT()
{
    // Get the object used to communicate with the server.  
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("FTP address");
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.UsePassive = false;    //tried it with true as well

    request.Credentials = new NetworkCredential("user", "pass");

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

    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);
    string result = (reader.ReadToEnd());
}

2 个答案:

答案 0 :(得分:0)

最终发现我正在将该站点发布到一个子文件夹中。 根文件夹可能包含较旧的版本。 无论如何,谢谢您的帮助!

答案 1 :(得分:-1)

如果只是从服务器FTP下载,我建议您为您的方法由Microsoft实现的此类WebClient

也许可以解决您的问题。

public static string[] GetTXT()
{   
    using (WebClient client = new WebClient())
    {
        var path = @"C:\local\path\file.txt";
        client.Credentials = new NetworkCredential("log", "pass");
        client.DownloadFile("ftp://ftp.example.com/remote/path/file.txt", path);
    }
    if (File.Exists(path))
    {

        return File.ReadAllLines(path);
    }
    //return something  
}