12

我想从 ftp 服务器下载文件。我已经编写了以下代码来从 ftp 下载文件

public void downloadFile(string FTPAddress, string filename, string username, string password, string destFile)
{
    try
    {
        FtpWebRequest request = FtpWebRequest.Create(FTPAddress + filename) as FtpWebRequest;
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.UseBinary = true;
        request.KeepAlive = false; //close the connection when done
        request.Timeout = 60000;
        //Streams
        using (var response = request.GetResponse())
        {

            using (Stream reader = response.GetResponseStream())
            {
                byte[] buffer = new byte[1024];
                using (Stream streamFile = File.Create(destFile))
                {
                    while (true)
                    {
                        int bytesRead = reader.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        else
                        {
                            streamFile.Write(buffer, 0, bytesRead);

                        }
                    }
                }
            }
        }
    }
    catch(Exception e)
    {
        Console.WriteLine(e.Message);             
    }
}     

但是当运行这段代码给我异常时:

底层连接已关闭:接收时发生意外错误。

有什么问题可以帮我...

4

1 回答 1

2

建议 1:明确将超时设置为无限。

request.Timeout = -1;

建议2:捕捉更具体的异常类型WebException,并检查WebExceptionStatus。

建议 3:打开 System.Net 的跟踪。

我已经为您的情况不适用的两个来源设置了 TraceLevel。

如何:配置网络跟踪

<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="includehex" maxdatasize="1024">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Cache">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.Http">
        <listeners>
          <add name="System.Net "/>
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
      <source name="System.Net.WebSockets">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="System.Net" value="Verbose"/>
      <add name="System.Net.Cache" value="Off"/>
      <add name="System.Net.Http" value="Off"/>
      <add name="System.Net.Sockets" value="Verbose"/>
      <add name="System.Net.WebSockets" value="Off"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
      />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
</configuration>
于 2015-05-01T05:47:09.090 回答