[英]Unable to connect to the remote server (ftp works in development but not in production)
我正在开发一个 asp.net mvc web 应用程序,该应用程序位于 web.com 托管的共享服务器上。 它在 IIS 7 和 .NET 4.0 上运行。
我收到错误消息“ [WebException:无法连接到远程服务器] System.Net.FtpWebRequest.GetResponse() ”,当我的弹出表单加载它的下拉列表时,它包含我的 ftp 站点中的目录名称。 我能够运行我的应用程序并连接到开发中的 ftp(使用 visual studio 2019)。 而且我能够将 te.net 连接到 ftp(默认端口 21)。
Controller 代码:
[NoCache]
[Authorize]
[HttpGet]
public ActionResult UploadFile()
{
ViewData["Folders"] = GetFolders();
return View();
}
private IEnumerable<SelectListItem> GetFolders()
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
request.KeepAlive = false;
request.Timeout = -1;
request.UsePassive = true;
request.Proxy = null;
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
List<SelectListItem> folders = new List<SelectListItem>();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string names = reader.ReadToEnd();
reader.Close();
response.Close();
var folderList =
new List<string>(names.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
.ToList());
folders = folderList.ConvertAll(a =>
{
return new SelectListItem()
{
Text = a.ToString(),
Value = a.ToString(),
Selected = false
};
});
return folders;
}
这是带有原始 ftpUrl 的 ftpwebrequest 日志:
System.Net Information: 0 : [11124] Current OS installation type is 'Server'.
System.Net Information: 0 : [27548]
FtpWebRequest#25592066::.ctor(ftp://ftp.mywebsite.com/ftp/)
System.Net Information: 0 : [27548]
FtpWebRequest#25592066::GetResponse(Method=NLST.)
System.Net Error: 0 : [27548] Exception in
FtpWebRequest#25592066::GetResponse - Unable to connect to the remote server.
at System.Net.FtpWebRequest.GetResponse()
这是我将 ftpUrl 更改为 root 时的 ftpwebrequest 日志:
System.Net Information: 0 : [11124] Current OS installation type is 'Server'.
System.Net Information: 0 : [20756]
FtpWebRequest#17494990::.ctor(ftp://ftp.mywebsite.com/)
System.Net Information: 0 : [20756]
FtpWebRequest#17494990::GetResponse(Method=NLST.)
System.Net Error: 0 : [20756] Exception in
FtpWebRequest#17494990::GetResponse - Unable to connect to the remote server.
at System.Net.FtpWebRequest.GetResponse()
这是我将 ftpUrl 更改为 IP 地址时的 ftpwebrequest 日志:
System.Net Information: 0 : [50288] Current OS installation type is 'Server'.
System.Net Information: 0 : [25964]
FtpWebRequest#40369685::.ctor(ftp://111.222.333.44/)
System.Net Information: 0 : [25964]
FtpWebRequest#40369685::GetResponse(Method=NLST.)
System.Net Error: 0 : [25964] Exception in
FtpWebRequest#40369685::GetResponse - Unable to connect to the remote server.
at System.Net.FtpWebRequest.GetResponse()
我是 C# 和 asp.net mvc 的新手,所以请多多包涵。 任何帮助将不胜感激!
FTP 端口可能会被过滤掉从您的服务器打开的连接。
一种选择是在该服务器上打开一个终端(不是从您的计算机或您的开发环境)并尝试运行 te.net 到 ftp 端点。
无论如何,如果您不能尝试并确保您使用的是正确的 FTP 端点,您可以将当前的GetFolders
代码替换为以下将测试连接性的代码:
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
private IEnumerable<SelectListItem> GetFolders()
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
request.KeepAlive = false;
request.Timeout = -1;
request.UsePassive = true;
request.Proxy = null;
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
List<SelectListItem> folders = new List<SelectListItem>();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string names = reader.ReadToEnd();
reader.Close();
response.Close();
var folderList =
new List<string>(names.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
.ToList());
folders = folderList.ConvertAll(a =>
{
return new SelectListItem()
{
Text = a.ToString(),
Value = a.ToString(),
Selected = false
};
});
return folders;
}
catch (Exception ex)
{
var hostname = (new Uri(ftpUrl)).Host;
bool pingable = false;
using (Ping pinger = new Ping())
{
try
{
PingReply reply = pinger.Send(hostname);
pingable = reply.Status == IPStatus.Success;
}
catch (Exception pEx)
{
throw new Exception($"Error pinging {hostname}: {pEx.ToString()}", pEx);
}
}
if (pingable)
{
using (TcpClient tc = new TcpClient())
{
tc.Connect(hostname, 20);
bool stat = tc.Connected;
if (!stat)
throw new Exception($"Cannot connect to {hostname} on port 20", ex);
tc.Close();
}
using (TcpClient tc = new TcpClient())
{
tc.Connect(hostname, 21);
bool stat = tc.Connected;
if (!stat)
throw new Exception($"Cannot connect to {hostname} on port 21", ex);
tc.Close();
}
throw new Exception($"Could connect to {hostname} on port 20 and 21", ex);
}
throw new Exception($"Could not ping {hostname}", ex);
}
}
新异常应该足以让您意识到问题所在。
hostname
”或“无法连接到端口 21 上的ftpUrl
”之类的错误,则端口被过滤,您应该检查是否有防火墙过滤它。 编辑:我刚刚意识到在ftpUrl
中你包含了类似“ftp://host/path”的东西。 这就解释了为什么你会得到“没有这样的主机是已知的”。 所以我只是编辑了代码以避免这种情况。
不管怎样,根据您的评论,FTP 正在同一台服务器上运行并检查您之前发布的错误(无法连接到远程服务器),我猜可能是 FTP 站点已停止。
如果 FTP 站点已停止,只需打开 IIS 管理器 (.netmgr),展开站点并确保 FTP 站点(它可能有任何名称,您需要检查绑定以确认它是 FTP)正在运行,否则启动它。
原来你的FTP服务器其实和你的web服务器是同一台机器。 所以它是与 FTP 服务器相关的localhost
主机。
通常,连接到本地主机服务器时应该没有任何问题。 如果它不起作用,则可能是提供者明确禁止本地主机连接(尽管这很奇怪)。 你真的确定web服务器和FTP服务器是同一个吗?
无论如何,使用 FTP 连接到本地机器是没有意义的。如果你直接在机器上,你可以直接访问文件——那些是本地文件。 使用System.IO
类,如File
或Directory
(特别是Directory.GetFiles
或类似的),而不是FtpWebRequest
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.