我正在开发一个asp.net mvc web应用程序,该应用程序位于由web.com托管的共享服务器上。它运行在IIS7和.NET 4.0上。
当我的弹出表单加载时,我得到了错误"WebException:无法连接到远程服务器System.Net.FtpWebRequest.GetResponse()“,它是下拉列表,其中包含了ftp站点中目录的名称。我能够运行我的应用程序并连接到正在开发中的ftp (使用visual studio 2019)。并且我能够将网络传输到ftp (默认端口21)。
控制器代码:
[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的新手,所以请容忍我。任何帮助都将非常感谢!
发布于 2020-10-11 02:16:15
对于从服务器打开的连接,可能会对FTP端口进行过滤。
一种选择是在该服务器上打开一个终端(不是从您的计算机或从dev环境),然后尝试将telnet运行到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管理器(inetmgr),展开站点并确保FTP站点(它可能有任何名称,您需要检查绑定以确认它正在运行),否则启动它。
发布于 2020-10-14 06:57:10
事实证明,您的FTP服务器实际上与您的web服务器是同一台机器。所以它是相对于FTP服务器的localhost。
通常,当连接到本地主机服务器时,不应该出现任何问题。如果它不能工作,那么提供程序很可能显式地禁止本地主机连接(尽管它很奇怪)。您真的确定web服务器和FTP服务器是相同的吗?
无论如何,使用FTP连接到本地计算机是没有意义的。如果您直接在机器上,您可以直接访问这些文件--这些是本地文件。使用System.IO类,比如File或Directory (特别是Directory.GetFiles或类似类),而不是FtpWebRequest。
https://stackoverflow.com/questions/64295022
复制相似问题