I want my program in C# to check if a website is online prior to executing, how would I make my program ping the website and check for a response in C#?
A Ping only tells you the port is active, it does not tell you if it's really a web service there.
My suggestion is to perform a HTTP HEAD request against the URL
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("your url"); request.AllowAutoRedirect = false; // find out if this site is up and don't follow a redirector request.Method = "HEAD"; try { response = request.GetResponse(); // do something with response.Headers to find out information about the request } catch (WebException wex) { //set flag if there was a timeout or some other issues }
This will not actually fetch the HTML page, but it will help you find out the minimum of what you need to know. Sorry if the code doesn't compile, this is just off the top of my head.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With