I'm using the following code to Validate URLs
private Boolean CheckURL(string url) { using (MyClient myclient = new MyClient()) { try { myclient.HeadOnly = true; // fine, no content downloaded string s1 = myclient.DownloadString(url); statusCode = null; return true; } catch (WebException error) { if (error.Response != null) { HttpStatusCode scode = ((HttpWebResponse)error.Response).StatusCode; if (scode != null) { statusCode = scode.ToString(); } } else { statusCode = "Unknown Error"; } return false; } } } class MyClient : WebClient { public bool HeadOnly { get; set; } protected override WebRequest GetWebRequest(Uri address) { WebRequest req = base.GetWebRequest(address); // req.Timeout = 3000; if (HeadOnly && req.Method == "GET") { req.Method = "HEAD"; } return req; } } This works fine for most of the cases,but for some URLs it returns False Positive Results. For Valid URLs(when I browse using chrome) the method Returns Not Found. Also for some URLs this method takes too much time to process.
What I'm doing wrong? Please advice..
UPDATE:
I'm checking the URLs from Multiple threads using Parallel,does this cause the problem?
public void StartThreads() { Parallel.ForEach(urllist, ProcessUrl); } private void ProcessUrl(string url) { Boolean valid = CheckURL(url); this.Invoke((MethodInvoker)delegate() { if (valid) { //URL is Valid } else { //URL is Invalid } }); } I'm starting the threads from a BackGround Worker to prevent UI Freezing
private void worker_DoWork(object sender, DoWorkEventArgs e) { StartThreads(); }
HttpUtility.UrlEncode(url)instead.myclient.DownloadString(HttpUtility.UrlEncode(url))?