1

Possible Duplicate:
Best way to test if a website is alive from a C# applicaiton

I need to find website is running in c# I am passing the website on port 80.

I am using the following code in c#

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://tester.co.tt");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I have given invalid website address, then also i am getting response is OK.

How can we check website is running?

I am getting the response.StatusCode is OK. when i browse the site,it is not avialable.

Community
  • 1
  • 1
user386258
  • 1,933
  • 8
  • 22
  • 28
  • When yiou say you are getting "repsonse is ok" do you mean the status code of the response is ok or just that it creates a response object that is not null? If the former then what is the body of the response? I'd guess you have a proxy or something that is returning a confusing status to you... – Chris Jan 18 '12 at 15:52
  • @chris I am getting the response.StatusCode is OK. when i browse the site,it is not avialable. – user386258 Jan 18 '12 at 15:56
  • @user386258: But in the `Response` object that has statuscode OK you can see the rest of the response (eg the HTML it is sending). Is this correctly what you would expect for the page (assuming you know what you are expecting)? – Chris Jan 18 '12 at 16:29

3 Answers3

0

you can use HttpWebResponse. HttpWebResponse Documentation

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83
  • -1: This is a very bad approach. What if the response code is any other code in the 200 or 300 family? – Jon Jan 18 '12 at 15:30
  • -1: `I have given invalid website address, then also i am getting response is OK.` – ANeves Jan 18 '12 at 15:32
  • +1, this is the standard method. 200 and 300 is rare and irrelevant for most sites. – javasocute Jan 18 '12 at 16:15
  • 2
    @jon if it's bad approach, then you can share your approach, so that we can also learn from you. – Ravi Gadag Jan 18 '12 at 16:19
  • @javasocute: You 've got to be kidding me. Try with `www.gmail.com` and see what happens. – Jon Jan 18 '12 at 16:22
  • @Jon: I would have thought that usually you know what the expectations of a site are and don't check things that will redirect you. Most of the time a status 200 on a chosen page does sound like it would do the trick. Its all kind of irrelevant though since he is getting a 200 when he doesn't expect one so this approach is wrong, even if it is the standard expected one. – Chris Jan 18 '12 at 16:33
  • @Ravi: Examine the HTTP response code for each request and make another request if necessary, until you reach a code that is "final"? There are **lots** of unknowns here, e.g. does HTTP 403 mean the site is up or not? – Jon Jan 18 '12 at 16:34
  • @Jon then, tho, i think there is no work around to determine properly ?. anyway i learnt some thing new from you :), though 2 downvotes for it, hurt a bit ;) – Ravi Gadag Jan 18 '12 at 16:41
  • @Ravi: I see you got +16 rep and some new information for typing a few lines. Doesn't look like a bad trade. ;) – Jon Jan 18 '12 at 16:53
0

If the invalid address entered is another website, than you will get a valid response (for instance if you wanted to check http://smackexchange.com, but typed http://stackexchange.com). I think you'd have better luck to request a specific asset from a website (i.e. a http://smackexchange.com/assets/pixel.gif) - but that will only work if you control the site

Jason
  • 15,915
  • 3
  • 48
  • 72
0

Use the built-in Ping class: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

NickV
  • 549
  • 7
  • 17
  • Ping will not tell rather the wesite is up or down, it only shows if the server respond to a ping. – Taco2 Mar 28 '17 at 13:41