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