0

I'm using the following code to check if a URL is Valid.This works as intended for Most URLs but produces False Positive Results in cases of HTTP 301 Responses.For example the URL http://www.amazon.com has 301 redirect in place. When using this online tool http://www.urlitor.com it shows HTTP Status (final destination) as 200.

I have set request.AllowAutoRedirect = true; in my code.But it does not seem to work as intended.Please advice.

try
            {
                var request = WebRequest.Create(url) as HttpWebRequest;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
                request.AllowAutoRedirect = true;
                using (var response = request.GetResponse())
                    return (response as HttpWebResponse).StatusCode == HttpStatusCode.OK;
            }
            catch (Exception)
            {
                return false;
            }
techno
  • 6,100
  • 16
  • 86
  • 192

0 Answers0