Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These solutions are pretty good, but they are forgetting that there may be other status codes than 200 OK. This is a solution that I've used on production environments for status monitoring and such. </p> <p>If there is a url redirect or some other condition on the target page, the return will be true using this method. Also, GetResponse() will throw an exception and hence you will not get a StatusCode for it. You need to trap the exception and check for a ProtocolError.</p> <p>Any 400 or 500 status code will return false. All others return true. This code is easily modified to suit your needs for specific status codes.</p> <pre><code>/// &lt;summary&gt; /// This method will check a url to see that it does not return server or protocol errors /// &lt;/summary&gt; /// &lt;param name="url"&gt;The path to check&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public bool UrlIsValid(string url) { try { HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; request.Timeout = 5000; //set the timeout to 5 seconds to keep the user from waiting too long for the page to load request.Method = "HEAD"; //Get only the header information -- no need to download any content using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { int statusCode = (int)response.StatusCode; if (statusCode &gt;= 100 &amp;&amp; statusCode &lt; 400) //Good requests { return true; } else if (statusCode &gt;= 500 &amp;&amp; statusCode &lt;= 510) //Server Errors { //log.Warn(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url)); Debug.WriteLine(String.Format("The remote server has thrown an internal error. Url is not valid: {0}", url)); return false; } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) //400 errors { return false; } else { log.Warn(String.Format("Unhandled status [{0}] returned for url: {1}", ex.Status, url), ex); } } catch (Exception ex) { log.Error(String.Format("Could not test url {0}.", url), ex); } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload