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

icanjj icanjj 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

//保存图片到本地并转换成水印图片
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);

程序已检查此张图片已存在,并且能访问取得后缀名,也不知道什么原因,总有些图片下载不了,当出现此问题时,
超过某个时间,就自动跳过下载这张图,或用其它方式处理,总之不让程序超时,有什么解决办法?
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
icanjj
.NET社区 62,271 成员 668,968 内容