PC Review


Reply
Thread Tools Rate Thread

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

 
 
news.microsoft.com
Guest
Posts: n/a
 
      22nd Jun 2004
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




 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jun 2004
news.microsoft.com <me@itsme_nospam.com> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
news.microsoft.com
Guest
Posts: n/a
 
      22nd Jun 2004
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



> 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.
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jun 2004
news.microsoft.com <me@itsme_nospam.com> wrote:
> 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/m...#windows.forms
for the threading aspect of this.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
news.microsoft.com
Guest
Posts: n/a
 
      22nd Jun 2004
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;

}


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect if I use "AES" or "TKIP" as encryption method for "WPA-PSK"? Client or AP requirement? Kevin McNeal Windows XP Networking 2 27th Feb 2009 10:30 AM
Disabling device type from "auto detect" to "none" has no effect onslave drives kimiraikkonen Windows XP General 1 14th Jan 2008 12:29 PM
"detect when printer has finished printing" +"ms access" =?Utf-8?B?TG91?= Microsoft Access VBA Modules 1 10th May 2006 12:41 PM
How to detect a "RadioButton" server control 's "Checked" property on the client side? bredal Jensen Microsoft ASP .NET 0 26th Apr 2004 01:09 PM
"Detect and Repair" causes Print Layout View "lines" spectralUV Microsoft Word New Users 2 13th Feb 2004 11:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:44 AM.