From 24471d2d55276fcecd905d70822ec749f5cd1f28 Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Thu, 6 Jul 2023 12:03:24 -0400 Subject: [PATCH 1/2] Added ability to set the maximum number of redirects --- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index eb29af86..9513641f 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -89,6 +89,7 @@ public partial class HtmlWeb private string _cachePath; private bool _fromCache; + private int _maxAutoRedirects = 50; private int _requestDuration; private Uri _responseUri; private HttpStatusCode _statusCode = HttpStatusCode.OK; @@ -802,6 +803,15 @@ internal static HttpClient GetSharedHttpClient(string userAgent) /// The automatic decompression. public DecompressionMethods AutomaticDecompression { get; set; } + /// + /// Maximum number of redirects that will be followed. Default value is 50. + /// + public int MaxAutoRedirects + { + set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } } + get { return _maxAutoRedirects; } + } + /// /// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite. /// @@ -1581,6 +1591,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc bool oldFile = false; req = WebRequest.Create(uri) as HttpWebRequest; + req.MaximumAutomaticRedirections = MaxAutoRedirects; req.Timeout = Timeout; req.Method = method; req.UserAgent = UserAgent; @@ -1853,6 +1864,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc using (var client = new HttpClient(handler)) { client.Timeout = TimeSpan.FromMilliseconds(Timeout); + handler.MaxAutomaticRedirections = MaxAutoRedirects; if(CaptureRedirect) { From ab1cb2ce2b504bff81e35ab3c883eb7a9809bb22 Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Thu, 6 Jul 2023 12:13:54 -0400 Subject: [PATCH 2/2] Added comments --- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index 9513641f..81247119 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -804,8 +804,10 @@ internal static HttpClient GetSharedHttpClient(string userAgent) public DecompressionMethods AutomaticDecompression { get; set; } /// - /// Maximum number of redirects that will be followed. Default value is 50. + /// Maximum number of redirects that will be followed. + /// To disable redirects, do not set the value to 0, please set CaptureRedirect to 'true'. /// + /// Must be greater than 0, Default is 50. public int MaxAutoRedirects { set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }