【问题标题】:Download time-out when connection is down [duplicate]连接断开时下载超时[重复]
【发布时间】:2012-03-08 17:10:02
【问题描述】:

可能重复:
Timeout when connection is gone. HELP ME PLEASE

我想在我的代码中设置一个超时。 当文件正在下载并且我没有互联网时,它会计数为 60 秒,如果连接没有恢复,则会发出一条消息。

代码如下:

    string novoNome;
    novoNome = strlocal + "\\" + zipNome;
    using (WebClient wcDownload = new WebClient())
    { 
          try
            {
                if (!Directory.Exists(strlocal))
                {
                    Directory.CreateDirectory(strlocal);
                }

                #region comunicação para download
                //string saida;
                // cria uma requisição do arquivo para download
                webRequest = (HttpWebRequest)WebRequest.Create(url);

                webResponse = (HttpWebResponse)webRequest.GetResponse();                    

                //Perguntar o tamanho do arquivo
                Int64 fileSize = webResponse.ContentLength;

                Uri uri = new Uri(url);

                // Abrindo arquivo para Download
                strResponse = wcDownload.OpenRead(uri);
                // Criando novo arquivo para salvar no HDD
                strLocal = new FileStream(novoNome, FileMode.Create, FileAccess.Write, FileShare.None);
                #endregion

                #region transferencia
                int bytesSize = 0;

                byte[] downBuffer = new byte[2048];                   

                try
                {
                    while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                    {                           
                        strLocal.Write(downBuffer, 0, bytesSize);
                        //if(this.IsAccessible)
                        this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize });
                        //wcDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wcDownload_DownloadProgressChanged);                              
                    }                        
                }
                catch (Exception e)
                {

                }
                #endregion                    
            }                
            finally
            {
                strResponse.Close();
                strLocal.Close();
            }
        }

谁能帮帮我?

【问题讨论】:

  • 发布的代码有什么问题?
  • 这应该由下划线的tcp连接默认处理。
  • 代码没有问题。我只想知道我必须更改或添加哪一行。

标签: c# download timeout


【解决方案1】:

您可以在 WebRequest 实例上设置超时。类似:webRequest.Timeout=60000; 如果超时,将抛出WebException。查看MSDN documentation 了解更多信息。

【讨论】:

    【解决方案2】:

    您应该将 Timeout 属性设置为 WebRequest 类。

    http://msdn.microsoft.com/en-us/library/system.net.webrequest.timeout.aspx

    【讨论】:

      【解决方案3】:

      您可以设置两个超时,一个是前面提到的设置 webRequest.Timeout 的答案。对您的情况有用的另一个是:

      webResponse.GetResponseStream().ReadTimeout = 60000;    
      

      这将在您的互联网连接中断 60 秒时引发异常......

      【讨论】:

      • 这正是我想要的!但是,我该如何处理这个异常呢?你能看到我的代码并告诉我我必须把这个代码行放在哪里吗?以及如何处理它抛出的异常。非常感谢!
      • 没用!请帮帮我!! T.T
      猜你喜欢
      • 2012-12-04
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 2017-01-23
      相关资源
      最近更新 更多