我有以下方法使用httpwebrequest从网址下载文件。我正在下载列表中包含的150个文件。这可能需要最多30分钟。当我运行我的服务时,我的网络请求不断超时,我不确定为什么?我猜它正在创建150个任务并尽可能多地处理,但在某些任务的超时期限之后超时。我的方法发生了什么?
try
{
//Download File here
Task t = new Task(() =>
{
byte[] lnBuffer;
byte[] lnFile;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}", Settings1.Default.WebPhotosLocation, f.FileName));
//Breaks on the line below
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
httpWebRequest.Timeout = 14400000;
httpWebRequest.KeepAlive = true;
using (BinaryReader binaryReader = new BinaryReader(httpWebResponse.GetResponseStream()))
{
using (MemoryStream memoryStream = new MemoryStream())
{
lnBuffer = binaryReader.ReadBytes(1024);
while (lnBuffer.Length > 0)
{
memoryStream.Write(lnBuffer, 0, lnBuffer.Length);
lnBuffer = binaryReader.ReadBytes(1024);
}
lnFile = new byte[(int)memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(lnFile, 0, lnFile.Length);
}
}
}
using (System.IO.FileStream lxFS = new FileStream(string.Format(@"{0}\{1}", fileDirectory, f.FileName), FileMode.Create))
{
lxFS.Write(lnFile, 0, lnFile.Length);
}
Log.WriteLine(string.Format("Downloaded File: {0}", f.FullName), Log.Status.Success);
filesDownloaded++;
});
t.Start();
listOfTasks.Add(t);
}
catch (Exception ex)
{
Log.WriteLine(string.Format("There has been an error Downloading File {0}. Error: {1}", f.FullName, ex), Log.Status.Error);
throw;
}
修改
在代码中声明的行抛出异常:
异常:类型' System.Net.WebException'的例外情况发生在 System.dll但未在用户代码中处理
其他信息:操作已超时
首次下载开始后3分20秒后抛出错误
答案 0 :(得分:0)
原来我在调用GetResponse后设置了Timeout值。将超时置于此方法调用之上是完美的。
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}", Settings1.Default.WebPhotosLocation, f.FileName));
httpWebRequest.Timeout = 14400000;
httpWebRequest.KeepAlive = true;
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
//Do stuff
}