Skip to content

Commit 2e98e14

Browse files
Add optional int? change for _maxAutoRedirects
1 parent 2cc71c3 commit 2e98e14

2 files changed

Lines changed: 39 additions & 25 deletions

File tree

src/HtmlAgilityPack.Shared/HtmlNode.Encapsulator.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
1+
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
22
// Website & Documentation: http://html-agility-pack.net
33
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
44
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
55
// More projects: http://www.zzzprojects.com/
6-
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
6+
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
77

88
#if !METRO && !NETSTANDARD1_3
99

@@ -29,7 +29,7 @@ public partial class HtmlNode
2929
/// <exception cref="XPathException">Why it's thrown.</exception>
3030
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
3131
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
32-
/// <exception cref="FormatException">Why it's thrown.</exception>
32+
/// <exception cref="FormatException">Why it's thrown.</exception>
3333
/// <exception cref="Exception">Why it's thrown.</exception>
3434
public T GetEncapsulatedData<T>()
3535
{
@@ -50,7 +50,7 @@ public T GetEncapsulatedData<T>()
5050
/// <exception cref="XPathException">Why it's thrown.</exception>
5151
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
5252
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
53-
/// <exception cref="FormatException">Why it's thrown.</exception>
53+
/// <exception cref="FormatException">Why it's thrown.</exception>
5454
/// <exception cref="Exception">Why it's thrown.</exception>
5555
public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
5656
{
@@ -72,7 +72,7 @@ public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
7272
/// <exception cref="XPathException">Why it's thrown.</exception>
7373
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
7474
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
75-
/// <exception cref="FormatException">Why it's thrown.</exception>
75+
/// <exception cref="FormatException">Why it's thrown.</exception>
7676
/// <exception cref="Exception">Why it's thrown.</exception>
7777
public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = null)
7878
{
@@ -175,7 +175,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
175175
// Property is None-IEnumerable HasXPath-user-defined class
176176
if (propertyInfo.PropertyType.IsDefinedAttribute(typeof(HasXPathAttribute)) == true)
177177
{
178-
HtmlDocument innerHtmlDocument = new HtmlDocument();
178+
HtmlDocument innerHtmlDocument = new HtmlDocument();
179179

180180
innerHtmlDocument.LoadHtml(Tools.GetHtmlForEncapsulation(htmlNode, xPathAttribute.NodeReturnType));
181181

@@ -192,7 +192,7 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
192192
{
193193
string result = string.Empty;
194194

195-
if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
195+
if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
196196
{
197197
result = Tools.GetNodeValueBasedOnXPathReturnType<string>(htmlNode, xPathAttribute);
198198
}
@@ -381,6 +381,9 @@ public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = n
381381
}
382382
#endregion targetObject_NOTDefined_XPath
383383
}
384+
385+
386+
384387
}
385388

386389

@@ -624,10 +627,12 @@ internal static IList GetNodesValuesBasedOnXPathReturnType(HtmlNodeCollection ht
624627

625628

626629
IList result = listGenericType.CreateIListOfType();
630+
627631
foreach (HtmlNode node in htmlNodeCollection)
628632
{
629-
result.Add(Convert.ChangeType(GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType), listGenericType));
633+
result.Add(Convert.ChangeType(GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType), listGenericType));
630634
}
635+
631636
return result;
632637
}
633638

@@ -724,7 +729,7 @@ internal static int CountOfIEnumerable<T>(this IEnumerable<T> source)
724729
counter++;
725730
}
726731
return counter;
727-
}
732+
}
728733

729734
/// <summary>
730735
/// Return html part of <see cref="HtmlNode"/> based on <see cref="ReturnType"/>
@@ -747,6 +752,8 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
747752
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
748753
};
749754
}
755+
756+
750757
}
751758

752759

@@ -756,7 +763,7 @@ internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnT
756763
public enum ReturnType
757764
{
758765
/// <summary>
759-
/// The text between the start and end tags of the object.
766+
/// The text between the start and end tags of the object.
760767
/// </summary>
761768
InnerText,
762769

@@ -855,18 +862,18 @@ public sealed class SkipNodeNotFoundAttribute : Attribute
855862
public class NodeNotFoundException : Exception
856863
{
857864
/// <summary>
858-
///
865+
///
859866
/// </summary>
860867
public NodeNotFoundException() { }
861868

862869
/// <summary>
863-
///
870+
///
864871
/// </summary>
865872
/// <param name="message"></param>
866873
public NodeNotFoundException(string message) : base(message) { }
867874

868875
/// <summary>
869-
///
876+
///
870877
/// </summary>
871878
/// <param name="message"></param>
872879
/// <param name="inner"></param>
@@ -880,18 +887,18 @@ public NodeNotFoundException(string message, Exception inner) : base(message, in
880887
public class NodeAttributeNotFoundException : Exception
881888
{
882889
/// <summary>
883-
///
890+
///
884891
/// </summary>
885892
public NodeAttributeNotFoundException() { }
886893

887894
/// <summary>
888-
///
895+
///
889896
/// </summary>
890897
/// <param name="message"></param>
891898
public NodeAttributeNotFoundException(string message) : base(message) { }
892899

893900
/// <summary>
894-
///
901+
///
895902
/// </summary>
896903
/// <param name="message"></param>
897904
/// <param name="inner"></param>
@@ -906,18 +913,18 @@ public class MissingXPathException : Exception
906913
{
907914

908915
/// <summary>
909-
///
916+
///
910917
/// </summary>
911918
public MissingXPathException() { }
912919

913920
/// <summary>
914-
///
921+
///
915922
/// </summary>
916923
/// <param name="message"></param>
917924
public MissingXPathException(string message) : base(message) { }
918925

919926
/// <summary>
920-
///
927+
///
921928
/// </summary>
922929
/// <param name="message"></param>
923930
/// <param name="inner"></param>
@@ -926,7 +933,7 @@ public MissingXPathException(string message, Exception inner) : base(message, in
926933

927934
}
928935

929-
#if FX20
936+
#if FX20
930937
namespace System.Runtime.CompilerServices
931938
{
932939
[AttributeUsage(AttributeTargets.Method |

src/HtmlAgilityPack.Shared/HtmlWeb.cs

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

9090
private string _cachePath;
9191
private bool _fromCache;
92-
private int _maxAutoRedirects = 50;
9392
private int _requestDuration;
9493
private Uri _responseUri;
9594
private HttpStatusCode _statusCode = HttpStatusCode.OK;
@@ -100,6 +99,7 @@ public partial class HtmlWeb
10099
private bool _usingCacheIfExists;
101100
private string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x";
102101
private int _timeout = 100000;
102+
private int? _maxAutoRedirects;
103103

104104
/// <summary>
105105
/// Occurs after an HTTP request has been executed.
@@ -807,8 +807,8 @@ internal static HttpClient GetSharedHttpClient(string userAgent)
807807
/// Maximum number of redirects that will be followed.
808808
/// To disable redirects, do not set the value to 0, please set CaptureRedirect to 'true'.
809809
/// </summary>
810-
/// <value>Must be greater than 0, Default is 50.</value>
811-
public int MaxAutoRedirects
810+
/// <value>Must be greater than 0.</value>
811+
public int? MaxAutoRedirects
812812
{
813813
set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _maxAutoRedirects = value; } }
814814
get { return _maxAutoRedirects; }
@@ -1593,7 +1593,10 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
15931593
bool oldFile = false;
15941594

15951595
req = WebRequest.Create(uri) as HttpWebRequest;
1596-
req.MaximumAutomaticRedirections = MaxAutoRedirects;
1596+
if (MaxAutoRedirects.HasValue)
1597+
{
1598+
req.MaximumAutomaticRedirections = MaxAutoRedirects.Value;
1599+
}
15971600
req.Timeout = Timeout;
15981601
req.Method = method;
15991602
req.UserAgent = UserAgent;
@@ -1866,7 +1869,11 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc
18661869
using (var client = new HttpClient(handler))
18671870
{
18681871
client.Timeout = TimeSpan.FromMilliseconds(Timeout);
1869-
handler.MaxAutomaticRedirections = MaxAutoRedirects;
1872+
1873+
if (MaxAutoRedirects.HasValue)
1874+
{
1875+
handler.MaxAutomaticRedirections = MaxAutoRedirects.Value;
1876+
}
18701877

18711878
if(CaptureRedirect)
18721879
{

0 commit comments

Comments
 (0)