這個是在網路上找到的code,再加上自己修改了一些,
比較完整一些,不多說,趕快來看看code
我將他寫成一個class,code如下:
class CheckUrl { public bool IsUrlAvailable(string url)//確認URL是有用的 { if (!url.StartsWith("http://") && !url.StartsWith("https://")) { MessageBox.Show("URL is Error"); return false; } else { //來源http://www.codekeep.net/snippets/cc6ab87f-1dc9-4eae-b14e-cea446767197.aspx try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse()) { if (rsp.StatusCode == HttpStatusCode.OK) { return true; } } } catch (WebException) { // Eat it because all we want to do is return false } // Otherwise MessageBox.Show("URL:'" + url + "'\nis Error"); return false; } } }出處:http://www.codekeep.net/snippets/cc6ab87f-1dc9-4eae-b14e-cea446767197.aspx
沒有留言:
張貼留言