diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index e8568044..eb29af86 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -98,6 +98,7 @@ public partial class HtmlWeb private bool _usingCacheAndLoad; private bool _usingCacheIfExists; private string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x"; + private int _timeout = 100000; /// /// Occurs after an HTTP request has been executed. @@ -801,6 +802,15 @@ internal static HttpClient GetSharedHttpClient(string userAgent) /// The automatic decompression. public DecompressionMethods AutomaticDecompression { get; set; } + /// + /// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite. + /// + public int Timeout + { + get { return _timeout; } + set { if (value <= 0 && value != -1) { throw new ArgumentOutOfRangeException(); } else { _timeout = value; } } + } + /// /// Gets or Sets a value indicating if document encoding must be automatically detected. /// @@ -1571,6 +1581,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc bool oldFile = false; req = WebRequest.Create(uri) as HttpWebRequest; + req.Timeout = Timeout; req.Method = method; req.UserAgent = UserAgent; req.AutomaticDecompression = AutomaticDecompression; @@ -1841,6 +1852,8 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc using (var handler = new HttpClientHandler()) using (var client = new HttpClient(handler)) { + client.Timeout = TimeSpan.FromMilliseconds(Timeout); + if(CaptureRedirect) { handler.AllowAutoRedirect = false;