Test if a website is alive from a C# application
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
As @Yanga mentioned, HttpClient is probably the more common way to do this now.
HttpClient client = new HttpClient();
var checkingResponse = await client.GetAsync(url);
if (!checkingResponse.IsSuccessStatusCode)
{
return false;
}
How would I check to see if website a is online
Just make a request to WebSite
private bool PingWebSite(string url)
{
try
{
WebRequest.Create(url).GetResponse();
return true;
}
catch
{
return false;
}
}
And then use it
var isWebSiteWorking = PingWebSite("http://stackoverflow.com");
How to check if website exists
You need to use the GetResponseAsync()
HttpWebResponse response = await webrequest.GetResponseAsync();
How do you check if a website is online in C#?
You have use System.Net.NetworkInformation.Ping
see below.
var ping = new System.Net.NetworkInformation.Ping();
var result = ping.Send("www.google.com");
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
return;
How to test if web site written in ASP.Net still alive?
What happens when your site fails? Does it return a 500 status code or timeout?
Another way to look at it: does it always do something expected if it succeeds?
You might call a URL in your web app that you know will either return a 200 response code or will have some expected HTML markup in the response if things are working fine.
Have your winform call this URL and examine the Response.status or the text in the output buffer for your expected markup. You should also create a timeout in your httprequest. If the page does not load within the timeout, you will get a web exception and you will know the site is failing.
Also, if you have the budget, there are external monitoring services like gomez.com that can automate this and provide reporting on site availability.
C# How can I check if a URL exists/is valid?
You could issue a "HEAD" request rather than a "GET"?
So to test a URL without the cost of downloading the content:
// using MyClient from linked post
using(var client = new MyClient()) {
client.HeadOnly = true;
// fine, no content downloaded
string s1 = client.DownloadString("http://google.com");
// throws 404
string s2 = client.DownloadString("http://google.com/silly");
}
You would try
/catch
around the DownloadString
to check for errors; no error? It exists...
With C# 2.0 (VS2005):
private bool headOnly;
public bool HeadOnly {
get {return headOnly;}
set {headOnly = value;}
}
and
using(WebClient client = new MyClient())
{
// code as before
}
Related Topics
Getting a System.Type from Type's Partial Name
Create a Coroutine to Fade Out Different Types of Object
When to Use Ilist and When to Use List
How to Get the Directory from a File's Full Path
How to Calculate the Ip Range When the Ip Address and the Netmask Is Given
Async/Await Different Thread Id
How to Set Conditional Breakpoints in Visual Studio
Does Lock() Guarantee Acquired in Order Requested
Force JSON.Net to Include Milliseconds When Serializing Datetime (Even If Ms Component Is Zero)
Regarding Local Variable Passing in Thread
How to Return a Specific Status Code and No Contents from Controller
C# Iterating Through an Enum? (Indexing a System.Array)
Method Overloading VS Optional Parameter in C# 4.0
What Is the Correct Way to Read a Serial Port Using .Net Framework