【发布时间】:2015-06-05 13:42:38
【问题描述】:
我正在尝试通过 FTP 从 MOXA UC8410 下载文件。我的代码不起作用。这是我的代码:
void download2()
{
Uri serverUri = new Uri("ftp://169.254.1.1/CFDisk/PCPACM/pcpacm.ini");
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("admin", "admin");
try
{
byte[] newFileData = request.DownloadData(serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
//Console.WriteLine(fileString);
}
catch (WebException e)
{
// Console.WriteLine(e.ToString());
MessageBox.Show(e.Response + e.Message);
}
}
我也试过这个:
void download()
{
try
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://169.254.1.1:21/CFDisk/PCPACM/pcpacm.ini"));
// using admin as the username and admin as the passward.
request.Credentials = new NetworkCredential("admin", "admin");
//request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
processingfile(reader);
responseStream.Close();
reader.Close();
}
catch (Exception e2)
{
MessageBox.Show("Not connected" , e2.Message);
}
}
代码到达
Stream responseStream = response.GetResponseStream();
它只是停止,它永远不会进入下一行。
输出说:
线程 0x175c 已退出,代码为 259 (0x103)。
线程 0x212c 已退出,代码为 259 (0x103)。
这没有帮助。
我可以使用命令提示符 ftp 到 MOXA UC8410,我可以使用 FileZilla 下载文件,但不使用我的代码。 Moxa UC8410 上没有防火墙,所以我的代码很可能有问题。
更新日期: 更新它正在工作!!!
但只有当我转到本地连接属性并将 Internet 协议版本 4(tcp/IPv4) 更改为
使用以下 IP 地址:
IP地址:169.254.1.5
子网掩码:225.225.0.0
有人知道为什么吗?有没有办法在我不必这样做的地方修复它?
为什么我必须把它们放在同一个子域上?
【问题讨论】: