Skip to content

Commit 24471d2

Browse files
committed
Added ability to set the maximum number of redirects
1 parent 058b50e commit 24471d2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/HtmlAgilityPack.Shared/HtmlWeb.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public partial class HtmlWeb
8989

9090
private string _cachePath;
9191
private bool _fromCache;
92+
private int _maxAutoRedirects = 50;
9293
private int _requestDuration;
9394
private Uri _responseUri;
9495
private HttpStatusCode _statusCode = HttpStatusCode.OK;
@@ -802,6 +803,15 @@ internal static HttpClient GetSharedHttpClient(string userAgent)
802803
/// <value>The automatic decompression.</value>
803804
public DecompressionMethods AutomaticDecompression { get; set; }
804805

806+
/// <summary>
807+
/// Maximum number of redirects that will be followed. Default value is 50.
808+
/// </summary>
809+
public int MaxAutoRedirects
810+
{
811+
set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }
812+
get { return _maxAutoRedirects; }
813+
}
814+
805815
/// <summary>
806816
/// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite.
807817
/// </summary>
@@ -1581,6 +1591,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
15811591
bool oldFile = false;
15821592

15831593
req = WebRequest.Create(uri) as HttpWebRequest;
1594+
req.MaximumAutomaticRedirections = MaxAutoRedirects;
15841595
req.Timeout = Timeout;
15851596
req.Method = method;
15861597
req.UserAgent = UserAgent;
@@ -1853,6 +1864,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
18531864
using (var client = new HttpClient(handler))
18541865
{
18551866
client.Timeout = TimeSpan.FromMilliseconds(Timeout);
1867+
handler.MaxAutomaticRedirections = MaxAutoRedirects;
18561868

18571869
if(CaptureRedirect)
18581870
{

0 commit comments

Comments
 (0)