How to Timeout a request using Html Agility Pack

前端 未结 4 667
情书的邮戳
情书的邮戳 2021-01-12 23:56

I\'m making a request to a remote web server that is currently offline (on purpose).

I\'d like to figure out the best way to time out the request. Basically if the

4条回答
  •  醉话见心
    2021-01-13 00:40

    Html Agility Pack is open souce. Thats why you may modify source yurself. For first add this code to class HtmlWeb:

    private int _timeout = 20000;
    
    public int Timeout 
        { 
            get { return _timeout; } 
            set
            {
                if (_timeout < 1) 
                    throw new ArgumentException("Timeout must be greater then zero.");
                _timeout = value;
            }
        }
    

    Then find this method

    private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc, IWebProxy proxy, ICredentials creds)
    

    and modify it:

    req = WebRequest.Create(uri) as HttpWebRequest;
    req.Method = method;
    req.UserAgent = UserAgent;
    req.Timeout = Timeout; //add this
    

    Or something like that:

    htmlWeb.PreRequest = request =>
                {
                    request.Timeout = 15000;
                    return true;
                };
    

提交回复
热议问题