今天突然出来了一个问题,URL地址调用导致程序卡死(原因是服务挂了,磁盘坏了)
然后想到了,再调用URL地址前先判断下地址是否有响应,这样不就可以解决问题了吗?
C# 代码:
///<summary>///检测URL地址是否可以访问///</summary>///<param name="Url">URL地址(以http://开头)</param>///<returns>返回OK</returns>///<example>IsUrlVist("http://www.baidu.com")</example>publicstaticstringIsUrlVisit(stringUrl)
{stringreVlue =null;try{
HttpWebRequest request= (HttpWebRequest)WebRequest.Create(newUri(Url));
request.Timeout=30;//超时时间ServicePointManager.Expect100Continue =false;
((HttpWebResponse)request.GetResponse()).Close();
reVlue="OK";
}catch(WebException ex)
{
reVlue=ex.ToString();
}returnreVlue;
}