【发布时间】:2015-02-09 13:11:59
【问题描述】:
这是代码:
HttpWebRequest request;
int currentIndex = 0;
void fileDownloadRadar(string uri, string fileName)
{
if (splash != null)
{
if (!splash.IsDisposed)
splash.UpdateProgressBar(0);
}
/*_request = WebRequest.Create(uri) as HttpWebRequest;
_request.CookieContainer = new CookieContainer();
_request.AllowAutoRedirect = true;
_responseAsyncResult = _request.BeginGetResponse(ResponseCallback, null);*/
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
long contentLength = response.ContentLength;
if (response.ContentType == "")
{
Logger.Write("ContentType is Empty download was not fine !!!!!");
}
if ((response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.Moved ||
response.StatusCode == HttpStatusCode.Redirect) &&
response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
{
Logger.Write("ContentType is not empty meaning download is fine");
using (Stream inputStream = response.GetResponseStream())
using (Stream outputStream = File.OpenWrite(fileName))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
currentIndex += bytesRead;
double percentage = (double)currentIndex / contentLength;
if (splash != null)
{
if (!splash.IsDisposed)
splash.UpdateProgressBar((int)(percentage * 100));
}
outputStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
if (splash != null)
{
if (!splash.IsDisposed)
{
splash.UpdateProgressBar(100);
}
}
}
}
else
{
timer1.Stop();
timer3.Start();
}
if (splash == null)
FinishWebRequest();
}
异常上线:
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
异常是:WebException
操作已超时
我看到当异常发生时变量 bytesRead 的值为 1360 缓冲区值为 4096
System.Net.WebException occurred
HResult=-2146233079
Message=The operation has timed out.
Source=System
StackTrace:
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at mws.Form1.fileDownloadRadar(String uri, String fileName) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 914
InnerException:
第 914 行是:
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
也许我应该把 try and catch 放在某个地方?
我的代码正在下载一个文件,然后当计时器计数 5 分钟时计时器正在运行,它再次下载相同的文件,以此类推,每 5 分钟一次不间断我正在调用此方法来下载文件。
编辑这是我根据答案更改后的代码:
HttpWebRequest request;
int currentIndex = 0;
void fileDownloadRadar(string uri, string fileName)
{
if (splash != null)
{
if (!splash.IsDisposed)
splash.UpdateProgressBar(0);
}
try
{
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.Timeout = 10000;
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
long contentLength = response.ContentLength;
if (response.ContentType == "")
{
Logger.Write("ContentType is Empty download was not fine !!!!!");
}
if ((response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.Moved ||
response.StatusCode == HttpStatusCode.Redirect) &&
response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
{
Logger.Write("ContentType is not empty meaning download is fine");
using (Stream inputStream = response.GetResponseStream())
using (Stream outputStream = File.OpenWrite(fileName))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
currentIndex += bytesRead;
double percentage = (double)currentIndex / contentLength;
if (splash != null)
{
if (!splash.IsDisposed)
splash.UpdateProgressBar((int)(percentage * 100));
}
outputStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
if (splash != null)
{
if (!splash.IsDisposed)
{
splash.UpdateProgressBar(100);
}
}
}
}
else
{
timer1.Stop();
timer3.Start();
}
if (splash == null)
FinishWebRequest();
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
{
Logger.Write(ex.Status.ToString());
}
}
}
但是在它工作了大约一个小时左右并下载了几次文件之后,我又遇到了异常超时:
操作已超时
这次上线了:
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
还有异常信息:
System.Net.WebException occurred
HResult=-2146233079
Message=The operation has timed out
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at mws.Form1.fileDownloadRadar(String uri, String fileName) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 899
InnerException:
我更改了响应并使用 using 还添加了:request.Timeout = 10000; 并添加了 try and catch。
【问题讨论】:
-
您需要在完成响应流后调用 response.Close()。
-
好的,我添加了一个 response.Close();在这一行之后: } while (bytesRead != 0);我还在 WebRequest.Create(uri); 之后的方法顶部添加了我补充说:request.Timeout = 10000;但是如果操作时间超过 10 秒会发生什么?那么它会再次抛出异常吗?
-
我刚刚发布了一个答案 - 它不应再引发超时。
-
在工作了大约一个小时左右之后,我再次遇到了超时异常。
-
在try catch 中,它没有到达catch,它首先抛出异常,只有当我继续它时它才到达catch。并且在我确实继续程序时继续工作。问题再次是为什么出现异常以及如何处理它,我猜 try 和 catch 没有帮助。