Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to set the maximum redirects #503

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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