[how-to] Detect if a URL is "alive"

N

news.microsoft.com

I want to make a boolean function to determine if a given URL is "alive",
where 'alive' means it does not result in DNS error, 404 error, or other
common errors.

I am currently using this code, but I'm not sure if it is the best way to do
it:

private bool IsURLAlive (string strURL)
{

WebClient myWebClient = new WebClient();
char[] buf = new char[128];


try
{

Stream myStream = myWebClient.OpenRead(strURL);

StreamReader sr = new StreamReader(myStream);

sr.ReadBlock(buf, 0, 127);

myStream.Close();

}

catch (System.Net.WebException webe)

{

return false;

}

return true;

}



I would like not to use exceptions, because I will be running a loop for,
say, 500 URLs.

Any ideas?

Thanks in advance,

Dan
 
J

Jon Skeet [C# MVP]

news.microsoft.com said:
I want to make a boolean function to determine if a given URL is "alive",
where 'alive' means it does not result in DNS error, 404 error, or other
common errors.

I am currently using this code, but I'm not sure if it is the best way to do
it:

<snip>

I don't know of any way of doing this without incurring an exception
other than implementing HTTP yourself.

You're right to be concerned about using exceptions for this kind of
detection, but I wouldn't worry too much about the performance side of
things - the time taken to throw the exception is likely to be
insignificant compared to the time taken to do the DNS lookup etc in
the first place.
 
N

news.microsoft.com

Thanks, Jon!

Just another question, if I may...

Inside my loop for 500 URLs, I would like to be populating a ListBox,
telling for each URL if it is alive. But is seems the ListBox gets updated
only at the end of the loop.
I remember in old VB we had a "ProcessMessages"(?) procedure. Is there
anything like that I can use?

Dan
 
J

Jon Skeet [C# MVP]

news.microsoft.com said:
Just another question, if I may...

Inside my loop for 500 URLs, I would like to be populating a ListBox,
telling for each URL if it is alive. But is seems the ListBox gets updated
only at the end of the loop.
I remember in old VB we had a "ProcessMessages"(?) procedure. Is there
anything like that I can use?

A few suggestions:

1) You should *definitely* not be doing this in the UI thread
2) Your worker thread can (either asynchronously or synchronously)
invoke a delegate on the UI thread to update the UI. You mustn't update
it from the worker thread directly.
3) You might want to consider batching the updates and using
Begin/EndUpdate on the list box

See
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms
for the threading aspect of this.
 
N

news.microsoft.com

Thanks again.

My final version:

public static HttpStatusCode GetURLStatus (string strURL)

{

HttpWebResponse myHttpWebResponse;

try

{

// Create a web request for the given URL.

HttpWebRequest myHttpWebRequest = (HttpWebRequest)
WebRequest.Create(strURL);

// Get the associated response for the above request.

myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

myHttpWebResponse.Close();

}

catch(WebException we)

{

return ((HttpWebResponse)we.Response).StatusCode;

}


return myHttpWebResponse.StatusCode;

}
 

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