0

We have a system that sometimes uses an iframe when we need content from a remote system. Typically, we wait for a feature to be available before adding the iframe. However, in this instance, this is a very useful feature, and our customers would like access as soon as it is available. So, we wanted to check the url, and if it returned a 404 error, display a "Coming Soon" message. As soon as the URL is available, our site will correctly use the new feature.

So, I found this method to determine if a URL is active:

private bool UrlExists(string website) {
    try {
        var request = System.Net.WebRequest.Create(website) as System.Net.HttpWebRequest;
        request.Method = "HEAD";
        using(var response = (System.Net.HttpWebResponse) request.GetResponse()) {
            return response.StatusCode != System.Net.HttpStatusCode.NotFound;
        }
    } catch {
        return false;
    }
}

Unfortunately, the URL initially returns a 200 response from the server, basically saying we hit a valid server, followed by a 404 error. The initial 200 response gives a false positive, even though there is no content to display. Is there a better approach? We can write either a javascript or C# function for this.

We considered trying the suggestion in this post How to detect an error 404 in an iframe? but we were concerned about swallowing actual errors once the feature is released.

Md. Faisal Habib
  • 1,014
  • 1
  • 6
  • 14
SlipEternal
  • 222
  • 2
  • 12

0 Answers0