I had to make a small adjustment to my originally published code
public JsonpResult About(string HomePageUrl) { Models.Pocos.About about = null; // ************* CHANGE HERE - added "timeout in milliseconds" to RemoteFileExists extension method. if (HomePageUrl.RemoteFileExists(1000)) { // Using the Html Agility Pack, we want to extract only the // appropriate data from the remote page. HtmlWeb hw = new HtmlWeb(); HtmlDocument doc = hw.Load(HomePageUrl); HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='wrapper1-border']"); if (node != null) { about = new Models.Pocos.About { html = node.InnerHtml }; } //todo: look into whether this else statement is necessary else { about = null; } } return this.Jsonp(about); }
Then I changed my RemoteFileExists extension method to have a timeout
public static bool RemoteFileExists(this string url, int timeout) { try {
In this approach, if my timeout fires before RemoteFileExists , you can determine the response of the header, then my bool will return false.
source share