namespace ChadSoft.Utils.Pingy.Pingers { using System; using System.Net; using System.Web; using ChadSoft.Logging; public class HttpPinger : UriPinger { public HttpPinger(Uri httpLocation) : base(httpLocation) { } public override PingResponse Ping() { PingResponse resp; try { // Create Uri request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Location); // Set the UserAgent just for kicks request.UserAgent = "HttpWebRequest"; // Send the request, get the response using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { resp = GetPingResponse(response); } } catch (WebException ex) { if(ex.Response != null) resp = GetPingResponse(ex.Response as HttpWebResponse); else throw; } catch (Exception ex) { Logger.Error(ex); resp = new PingResponse(ex); } return resp; } public override void Ping(PingerCallback callback) { throw new NotImplementedException(); } internal static PingResponse GetPingResponse(HttpWebResponse response) { PingResponse pingResponse; switch (response.StatusCode) { case HttpStatusCode.OK: // The Error page on the IG site returns an "OK"... but it's not. if (response.ResponseUri.AbsoluteUri.Contains("aspxerrorpath")) pingResponse = new PingResponse(PingStatus.Error); else { pingResponse = new PingResponse(PingStatus.Exists); using (System.IO.Stream stream = response.GetResponseStream()) pingResponse.CalculateChecksum(stream); } break; case HttpStatusCode.NotFound: pingResponse = new PingResponse(PingStatus.NotFound); break; default: pingResponse = new PingResponse( PingStatus.Unknown, String.Format("Status Code: {0}", response.StatusCode)); break; } return pingResponse; } } }