今天突然出來了一個問題,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; }