C# Check if a url is reachable with 200 OK response

C# Check if a url is reachable with 200 OK response

Sedat Ozdemir - c# Coding.jpg

Hello guys, this is my first post. I have a function about checking website reachability with 200 OK code. If you don’t have information about HTTP codes, I recommend you read this table.

ServicePointManager.MaxServicePointIdleTime = 1000;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Timeout = 15000;
request.Method = "GET";
System.Net.ServicePointManager.ServerCertificateValidationCallback = 
delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
 {
     return true;
 };
 System.Net.ServicePointManager.Expect100Continue = false;
 try
 {
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
     {
         if (response.StatusCode == HttpStatusCode.OK)
         {
            return true;
         }
         else
         {
            return false;
         }
     }
 }
 catch (Exception e)
 {
     return false;
 }
}

private void button1_Click(object sender, EventArgs e)
    {
      if(ApiServiceReachable("https://www.google.com.tr/") == true)
      {
                Console.WriteLine("Success !");
      }
    }

 

Tarafından yazıldı
Sedat Ozdemir
Bir yorum bırak

Teşekkürler!

Bloğumu ziyaret ettiğiniz ve yazılarımı okuduğunuz için teşekkürler!