下载远程图片时提示:System.Net.WebException: 操作已超时,如何解决?

icanjj 2009-07-04 02:39:09

//保存图片到本地并转换成水印图片
public static void savepic(string ohtml,string syimgPath, string imgurlPath, string tempPath)
{

//Response.Write(ohtml);
System.Text.RegularExpressions.MatchCollection m;
//提取字符串的图片
m = Regex.Matches(ohtml.ToLower(), "(<img).*?>");
for (int i = 0; i < m.Count; i++)
{
string ostr = m[i].ToString();
//提取图片的地址
System.Text.RegularExpressions.MatchCollection m2;

//以下的正则只能替换图片地址有双引号的
m2 = Regex.Matches(ostr, "(src=)['|\"].*?['\"]");


//picname = Guid.NewGuid().ToString();

for (int j = 0; j < m2.Count; j++)
{
string strTemp = m2[j].ToString();
strTemp = strTemp.Replace("src=", "");
strTemp = strTemp.Replace("\"", "");
strTemp = strTemp.Replace("'", "");
strTemp = strTemp.Replace("@", "");
strTemp = strTemp.Replace("v=0", "");
strTemp = strTemp.Replace("?", "");
//strTemp = strTemp.Replace("&", "&");

//Response.Write(strTemp+ "<br>");


//Response.Write(m2.ToString());
System.Net.WebClient wc = new System.Net.WebClient();

//在这里检查路径
bool a = GetUrlError(strTemp);
if (a == true)
{

//图片名称
string picname = Path.GetFileName(strTemp);

string hz = getImgHz(strTemp);//后缀
string picnameex = Path.GetExtension(strTemp);

string webFilePath_syyt = syimgPath; // 服务器端水印图路径(图片)
if (picnameex.Length == 0)
{

string tempPath2 = null;
//string nn = imgurlPath + picname + ".jpg";
tempPath2 = tempPath + picname + hz;


System.Net.ServicePointManager.DefaultConnectionLimit = 500;
//下载图片,还需要解决“System.Net.WebException: 操作已超时。”问题
wc.DownloadFile(strTemp, tempPath2);

}
else
{

string tempPath2 = null;
//string nn = imgurlPath + picname + ".jpg";
tempPath2 = tempPath + picname;
//下载图片

wc.DownloadFile(strTemp, tempPath2);


}

}
}



}

}


当某张图片下载不了时,就会提示“System.Net.WebException: 操作已超时”
wc.DownloadFile(strTemp, tempPath2);

程序已检查此张图片已存在,并且能访问取得后缀名,也不知道什么原因,总有些图片下载不了,当出现此问题时,
超过某个时间,就自动跳过下载这张图,或用其它方式处理,总之不让程序超时,有什么解决办法?
...全文
802 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiangx 2009-08-19
  • 打赏
  • 举报
回复
//以下的正则只能替换图片地址有双引号的
m2 = Regex.Matches(ostr, "(src=)['|\"].*?['\"]");
中无双引号的正则式怎么写啊
谢谢
真相重于对错 2009-07-05
  • 打赏
  • 举报
回复
如果ie下载没有问题,用webrequest 把请求头参数改为同ie一致
wuyq11 2009-07-05
  • 打赏
  • 举报
回复
通过WebClient wc = new WebClient();
wc.DownloadFile("");
下载或用while操作几次,不能下载异常
可能是网站设置了盗链,防止下载
cpio 2009-07-05
  • 打赏
  • 举报
回复
try
{
wc.DownloadFile(strTemp, tempPath2);
}
catch (WebException e)
{
continue;
}
蒋晟 版主 2009-07-05
  • 打赏
  • 举报
回复
重试
重试若干次如果还是下载不了,抛异常
iyori 2009-07-04
  • 打赏
  • 举报
回复
设置超时时间长一点

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧