Skip to content

Commit

Permalink
Added ability to set the maximum number of redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
basicn86 committed Jul 6, 2023
1 parent 058b50e commit 24471d2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -802,6 +803,15 @@ internal static HttpClient GetSharedHttpClient(string userAgent)
/// <value>The automatic decompression.</value>
public DecompressionMethods AutomaticDecompression { get; set; }

/// <summary>
/// Maximum number of redirects that will be followed. Default value is 50.
/// </summary>
public int MaxAutoRedirects
{
set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }
get { return _maxAutoRedirects; }
}

/// <summary>
/// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite.
/// </summary>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 24471d2

Please sign in to comment.