1

MVC4 应用程序在 HomeController 的操作中从 FTP 服务器下载文件。使用 Visual Studio 2010 网络服务器和本地网络服务器 IIS Express 8 它可以工作。但是当我使用带有 ApplicationPoolIdenty 的本地网络服务器 IIS 7.5 时,下载不起作用:我没有收到命令“response = reqFTP.GetResponse()”的响应,并且出现以下异常:“操作有时间到”)。

当我关闭 Windows 防火墙时,它可以工作。我需要做什么?

Function FtpStart() As ActionResult
        Dim downloadFiles As String()
        Dim result As New StringBuilder()
        Dim response As WebResponse = Nothing
        Dim reader As StreamReader = Nothing
        Try
            Dim reqFTP As FtpWebRequest
            reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://adress")),FtpWebRequest)
            reqFTP.UseBinary = True
            reqFTP.Credentials = New NetworkCredential("user", "pw")
            reqFTP.EnableSsl = False
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory
            reqFTP.Proxy = Nothing
            reqFTP.KeepAlive = False
            reqFTP.UsePassive = False
            Debug.WriteLine("Get Response: ")
            response = reqFTP.GetResponse()
            Debug.WriteLine("Response received")
            reader = New StreamReader(response.GetResponseStream())
            Dim line As String = reader.ReadLine()
            While line IsNot Nothing
                result.Append(line)
                result.Append(vbLf)
                line = reader.ReadLine()
                Debug.WriteLine("File: " & line)
            End While
            result.Remove(result.ToString().LastIndexOf(ControlChars.Lf), 1)
            Return RedirectToAction("Index")
        Catch ex As Exception
            Debug.WriteLine(ex.ToString())
            Return RedirectToAction("Index")
        End Try
    End Function
4

1 回答 1

0

今天早些时候我自己在我的网络服务器上遇到了这个问题。您必须在防火墙上为端口 20 和 21 的 TCP 入站规则设置规则。它应该显示本地端口 = 所有端口,远程端口 = 20-21。还要确保在高级选项卡上,域、私有和公共的所有三个复选标记都被选中。我的防火墙已经为端口 21 设置了 FTP 防火墙规则,但不是 20,只要我为端口 20 设置了规则,它就起作用了。

希望这可以帮助

于 2014-12-16T01:10:49.133 回答