Is there a faster way to check if an external web page exists?

前端 未结 5 876
感情败类
感情败类 2021-02-07 12:23

I wrote this method to check if a page exists or not:

protected bool PageExists(string url)
{
try
    {
        Uri u = new Uri(url);
        WebRequest w = WebR         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 13:01

    1. You could do it using asynchronous way, because now you are waiting for results after each request. For few pages, you could just throw your function in ThreadPool, and wait for all requests to finish. For more requests, you could use asynchronous methods for your ResponseStream() (BeginRead etc.).
    2. The other thing that can help you (help me for sure) is to clear .Proxy property:
    w.Proxy = null;

    Without this, at least 1st request is much slower, at least on my machine.
    3. You can not download whole page, but download only header, by setting .Method to "HEAD".

提交回复
热议问题