Skip to content

Commit

Permalink
Merge pull request #503 from basicn86/master
Browse files Browse the repository at this point in the history
Added ability to set the maximum redirects
  • Loading branch information
JonathanMagnan authored Jul 19, 2023
2 parents c12580b + ab1cb2c commit e4e5121
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 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,17 @@ 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.
/// To disable redirects, do not set the value to 0, please set CaptureRedirect to 'true'.
/// </summary>
/// <value>Must be greater than 0, Default is 50.</value>
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 +1593,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 +1866,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 e4e5121

Please sign in to comment.