【发布时间】:2023-03-14 09:19:01
【问题描述】:
我正在尝试使用 foreach 将图像链接列表下载到我的服务器(最多 40 个链接)。
在我的情况下,有时链接存在,但我不知道为什么它会捕获并取消下一个链接的下载。也许它需要等待一点?因为当我调试应用程序时,我看到链接是应用程序跳过并去捕获的可用但有时它在我的浏览器中几秒钟后打开,所以我尝试下载的服务器的响应时间有时需要更多时间来加载和打开链接。
string newPath = "~/data/" + model.PostID + "/" + name + "/";
//test1 is a list of links
foreach (var item1 in test1)
{
HttpWebRequest request = WebRequest.Create(item1) as HttpWebRequest; request.Method = "HEAD";
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
var webClient = new WebClient();
string path = newPath + i + ".jpg";
webClient.DownloadFileAsync(new Uri(item1), Server.MapPath(path));
string newlinks = "https://example.com/data/" + chapter.PostID + "/" + name + "/" + i + ".jpg";
allimages = allimages + newlinks + ',';
response.Close();
i++;
}
}
catch
{
break;
}
}
现在我的主要目标是解决这个问题,但正如我在调试中看到的那样:
我尝试下载的图片链接存在
有时需要更多时间来响应
那么我该如何解决这个问题?当下载取消并且存在链接时,我该怎么办?
【问题讨论】:
-
跟踪失败的下载,然后重试(即额外循环)。