Skip to content

Commit e4e5121

Browse files
Merge pull request #503 from basicn86/master
Added ability to set the maximum redirects
2 parents c12580b + ab1cb2c commit e4e5121

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/HtmlAgilityPack.Shared/HtmlWeb.cs

Lines changed: 14 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,17 @@ 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.
808+
/// To disable redirects, do not set the value to 0, please set CaptureRedirect to 'true'.
809+
/// </summary>
810+
/// <value>Must be greater than 0, Default is 50.</value>
811+
public int MaxAutoRedirects
812+
{
813+
set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }
814+
get { return _maxAutoRedirects; }
815+
}
816+
805817
/// <summary>
806818
/// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite.
807819
/// </summary>
@@ -1581,6 +1593,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
15811593
bool oldFile = false;
15821594

15831595
req = WebRequest.Create(uri) as HttpWebRequest;
1596+
req.MaximumAutomaticRedirections = MaxAutoRedirects;
15841597
req.Timeout = Timeout;
15851598
req.Method = method;
15861599
req.UserAgent = UserAgent;
@@ -1853,6 +1866,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
18531866
using (var client = new HttpClient(handler))
18541867
{
18551868
client.Timeout = TimeSpan.FromMilliseconds(Timeout);
1869+
handler.MaxAutomaticRedirections = MaxAutoRedirects;
18561870

18571871
if(CaptureRedirect)
18581872
{

0 commit comments

Comments
 (0)