// 设置要检查的 URL string url = "https://example.com";
// 创建 HttpWebRequest 对象 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// 设置请求信息 request.Method = "HEAD"; request.Timeout = 5000; // 设置 SSL/TLS 验证方式 request.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
// 发送请求并获取响应 HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; }
// 检查响应状态码 if (response.StatusCode == HttpStatusCode.OK) { Console.WriteLine("URL is available."); } else { Console.WriteLine("URL is not available."); }
import requests
url = "https://example.com"
response = requests.head(url, verify=False)
if response.status_code == requests.codes.ok: print("URL is available.") else: print("URL is not available.")