Checking if a website is online from a windows app

M

MarkusJNZ

Hi, I need to write some code to ensure that a website is still
online.

I was just going to write some code (Using System.NET webrequest) to
retrieve the HTML from a website and if the length was 0 then the
website was unavailable;

However, if I get a 404 error then obviously the above would not work.

I also thought of just using a ping but again some websites may be
online but not return a ping request (e.g. Microsoft).

Does anyone have any better ideas about how I can definately tell if a
website is available?

Thanks
Markus
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Hi, I need to write some code to ensure that a website is still
online.

I was just going to write some code (Using System.NET webrequest) to
retrieve the HTML from a website and if the length was 0 then the
website was unavailable;

However, if I get a 404 error then obviously the above would not work.

I also thought of just using a ping but again some websites may be
online but not return a ping request (e.g. Microsoft).

Does anyone have any better ideas about how I can definately tell if a
website is available?

I do not see what is wrong with the WebRequest approach !

0 bytes *or* exception means a problem

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Arne said:
I do not see what is wrong with the WebRequest approach !

0 bytes *or* exception means a problem

Code snippet catching exception:

public static bool Test(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "HEAD";
try
{
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
resp.Close();
return true;
}
catch
{
return false;
}
}

Arne
 
P

Peter Duniho

[...]
Does anyone have any better ideas about how I can definately tell if a
website is available?

First, you need to define "available" and "website". Are you talking
about a specific page at a specific URL? Or simply any response at all
from a web server at a specific address?

For example, a 404 error may in fact give you information that you want,
assuming it came from the web server. The URL might not exist, but at
least you know the server is there (assuming the 404 didn't come from
some intermediate proxy or something).

This seems to me to be a good example of a question that is in need of
more details. What exactly is the information you are trying to find
out here?

Pete
 
M

MarkusJNZ

[...]
Does anyone have any better ideas about how I can definately tell if a
website is available?

First, you need to define "available" and "website". Are you talking
about a specific page at a specific URL? Or simply any response at all
from a web server at a specific address?

For example, a 404 error may in fact give you information that you want,
assuming it came from the web server. The URL might not exist, but at
least you know the server is there (assuming the 404 didn't come from
some intermediate proxy or something).

This seems to me to be a good example of a question that is in need of
more details. What exactly is the information you are trying to find
out here?

Pete

Hi, very good point Pete. I need to know if the actual website is
available and going with that if the server is unavailable then
obviously the website will be unavailable as well.

Thanks for your help everyone
Markus
 
P

Peter Duniho

Hi, very good point Pete. I need to know if the actual website is
available and going with that if the server is unavailable then
obviously the website will be unavailable as well.

So maybe you can explain why getting a 404 error isn't useful
information to you. If you get a 404 error, doesn't that mean that the
website is not available, using the definitions you've provided here?

There are a variety of errors that you might get trying to retrieve a
specific URL. However, it seems to me that as long as you don't get any
error, the website is available, and if you do get any error, it's not.

If that's not suitable for your needs, IMHO you still need to be more
specific about what information it is exactly that you want.

Pete
 
M

MarkusJNZ

So maybe you can explain why getting a 404 error isn't useful
information to you. If you get a 404 error, doesn't that mean that the
website is not available, using the definitions you've provided here?

There are a variety of errors that you might get trying to retrieve a
specific URL. However, it seems to me that as long as you don't get any
error, the website is available, and if you do get any error, it's not.

If that's not suitable for your needs, IMHO you still need to be more
specific about what information it is exactly that you want.

Pete

Hi Pete, a 404 error is fine but I am not sure if a 404 will return a
string length > 0(?) from the webrequest. If so checking the sring
length would fail even though the website was still alive.

This post was to ask if I am doing things the right way or if someone
can recommend a better way to do things.

For all I know there may have been an obscure method buried deep in
the .NET 2.0 framework which did what I am going to do manually :)

Thanks for ally our help
Mark
 
P

Peter Duniho

Hi Pete, a 404 error is fine but I am not sure if a 404 will return a
string length > 0(?) from the webrequest. If so checking the sring
length would fail even though the website was still alive.

Why does it matter? If you get a 404 error, then that itself tells you
the information you want to know. Why would you care about any data
past the error response, even if it exists?
This post was to ask if I am doing things the right way or if someone
can recommend a better way to do things.

Well, I would agree that simply checking the length of the response
isn't an appropriate solution.
For all I know there may have been an obscure method buried deep in
the .NET 2.0 framework which did what I am going to do manually :)

I don't have any first-hand experience with the HttpWebRequest class,
but it looks to me as though if any HTTP error occurs when you call
GetResponse(), a WebException will be thrown. You can look at the
Response property of the WebException to see the details of the error,
but in your case the mere fact that the exception is thrown would
probably suffice.

Are you finding that you have a situation when the web site is not
available and yet an exception is not thrown when you call GetResponse()?

Pete
 
M

MarkusJNZ

Why does it matter? If you get a 404 error, then that itself tells you
the information you want to know. Why would you care about any data
past the error response, even if it exists?


Well, I would agree that simply checking the length of the response
isn't an appropriate solution.


I don't have any first-hand experience with the HttpWebRequest class,
but it looks to me as though if any HTTP error occurs when you call
GetResponse(), a WebException will be thrown. You can look at the
Response property of the WebException to see the details of the error,
but in your case the mere fact that the exception is thrown would
probably suffice.

Are you finding that you have a situation when the web site is not
available and yet an exception is not thrown when you call GetResponse()?

Pete

Hi Pete, I'll give it a whirl and see how I go. Thanks again for your
help

Regards
Markus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top