下载远程图片时提示:System.Net.WebException: 操作已超时,如何解决?
//保存图片到本地并转换成水印图片
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);
程序已检查此张图片已存在,并且能访问取得后缀名,也不知道什么原因,总有些图片下载不了,当出现此问题时,
超过某个时间,就自动跳过下载这张图,或用其它方式处理,总之不让程序超时,有什么解决办法?
...全文
相关推荐
关于System.Net.WebException The operation has timed out,即网络操作超时的异常
"本文主要讨论了在编程过程中遇到的`System.Net.WebException: The operation has timed out`异常,即网络操作超时的问题。这种异常通常由于多种原因导致
System.Net.WebException(解决方案).md
该异常属于System.Net命名空间下的一个类,通常需要开发者进行适当的异常处理以避免程序中断。在解决System.Net.WebException时,首先应当通过try-catch语句来捕获异常。
asp.net 使用url/ftp路径下载远程文档
WebClient是一个高度封装的类,提供了简单的HTTP和FTP操作,包括下载、上传、浏览网页等功能。下面我们将分步骤介绍如何使用WebClient下载远程文档。1.
asp.net(c#)判断远程图片是否存在
HttpWebRequest类是System.Net命名空间下的一个类,提供了发送HTTP请求的功能。
C# POST请求超时解决[项目源码]
然而,开发者在使用request.GetResponse()方法时可能会遇到超时异常,具体表现为“System.Net.WebException: The operation has timed out


