From cc475236f382b53212608b9efdae6603fd843ff3 Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Sat, 1 Jul 2023 16:40:09 -0400 Subject: [PATCH 1/5] Added ability to set the timeout of an HTTP request. --- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 13 ++ src/UpgradeLog.htm | 282 ++++++++++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 src/UpgradeLog.htm diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index e8568044..1c7fcad3 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -98,6 +98,7 @@ public partial class HtmlWeb private bool _usingCacheAndLoad; private bool _usingCacheIfExists; private string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x"; + private int _timeout = 100000; /// /// Occurs after an HTTP request has been executed. @@ -801,6 +802,15 @@ internal static HttpClient GetSharedHttpClient(string userAgent) /// The automatic decompression. public DecompressionMethods AutomaticDecompression { get; set; } + /// + /// Gets or sets the timeout value in milliseconds. + /// + public int Timeout + { + get { return _timeout; } + set { if (value < 0) { _timeout = 0; } else { _timeout = value; } } + } + /// /// Gets or Sets a value indicating if document encoding must be automatically detected. /// @@ -1571,6 +1581,7 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc bool oldFile = false; req = WebRequest.Create(uri) as HttpWebRequest; + req.Timeout = Timeout; req.Method = method; req.UserAgent = UserAgent; req.AutomaticDecompression = AutomaticDecompression; @@ -1841,6 +1852,8 @@ private HttpStatusCode Get(Uri uri, string method, string path, HtmlDocument doc using (var handler = new HttpClientHandler()) using (var client = new HttpClient(handler)) { + client.Timeout = TimeSpan.FromMilliseconds(Timeout); + if(CaptureRedirect) { handler.AllowAutoRedirect = false; diff --git a/src/UpgradeLog.htm b/src/UpgradeLog.htm new file mode 100644 index 00000000..c3131809 --- /dev/null +++ b/src/UpgradeLog.htm @@ -0,0 +1,282 @@ + + + + Migration Report +

+ Migration Report -

Overview

ProjectPathErrorsWarningsMessages
HtmlAgilityPack.UAP10HtmlAgilityPack.UAP10\HtmlAgilityPack.UAP10.csproj100
[Shared MSBuild Project Files][Shared MSBuild Project Files]000
HtmlAgilityPack.NetCore45HtmlAgilityPack.NetCore45\HtmlAgilityPack.NetCore45.csproj000
HtmlAgilityPack.NetStandard1_3HtmlAgilityPack.NetStandard1_3\HtmlAgilityPack.NetStandard1_3.csproj000
HtmlAgilityPack.NetStandard1_6HtmlAgilityPack.NetStandard1_6\HtmlAgilityPack.NetStandard1_6.csproj000
HtmlAgilityPack.NetStandard2_0HtmlAgilityPack.NetStandard2_0\HtmlAgilityPack.NetStandard2_0.csproj000
HtmlAgilityPack.Portable-wp8HtmlAgilityPack.Portable-wp8\HtmlAgilityPack.Portable-wp8.csproj000
HtmlAgilityPack.Portable-wp81HtmlAgilityPack.Portable-wp81\HtmlAgilityPack.Portable-wp81.csproj000
HtmlAgilityPack.SharedHtmlAgilityPack.Shared\HtmlAgilityPack.Shared.shproj000
HtmlAgilityPack.Tests.NetStandard2_0Tests\HtmlAgilityPack.Tests.NetStandard2_0\HtmlAgilityPack.Tests.NetStandard2_0.csproj000
SolutionHtmlAgilityPack.Master.sln001

Solution and projects

\ No newline at end of file From fdf342b2ad687a2226fba62bd66a012142446b58 Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Sat, 1 Jul 2023 16:45:30 -0400 Subject: [PATCH 2/5] Removed unncessary file. --- src/UpgradeLog.htm | 282 --------------------------------------------- 1 file changed, 282 deletions(-) delete mode 100644 src/UpgradeLog.htm diff --git a/src/UpgradeLog.htm b/src/UpgradeLog.htm deleted file mode 100644 index c3131809..00000000 --- a/src/UpgradeLog.htm +++ /dev/null @@ -1,282 +0,0 @@ - - - - Migration Report -

- Migration Report -

Overview

ProjectPathErrorsWarningsMessages
HtmlAgilityPack.UAP10HtmlAgilityPack.UAP10\HtmlAgilityPack.UAP10.csproj100
[Shared MSBuild Project Files][Shared MSBuild Project Files]000
HtmlAgilityPack.NetCore45HtmlAgilityPack.NetCore45\HtmlAgilityPack.NetCore45.csproj000
HtmlAgilityPack.NetStandard1_3HtmlAgilityPack.NetStandard1_3\HtmlAgilityPack.NetStandard1_3.csproj000
HtmlAgilityPack.NetStandard1_6HtmlAgilityPack.NetStandard1_6\HtmlAgilityPack.NetStandard1_6.csproj000
HtmlAgilityPack.NetStandard2_0HtmlAgilityPack.NetStandard2_0\HtmlAgilityPack.NetStandard2_0.csproj000
HtmlAgilityPack.Portable-wp8HtmlAgilityPack.Portable-wp8\HtmlAgilityPack.Portable-wp8.csproj000
HtmlAgilityPack.Portable-wp81HtmlAgilityPack.Portable-wp81\HtmlAgilityPack.Portable-wp81.csproj000
HtmlAgilityPack.SharedHtmlAgilityPack.Shared\HtmlAgilityPack.Shared.shproj000
HtmlAgilityPack.Tests.NetStandard2_0Tests\HtmlAgilityPack.Tests.NetStandard2_0\HtmlAgilityPack.Tests.NetStandard2_0.csproj000
SolutionHtmlAgilityPack.Master.sln001

Solution and projects

\ No newline at end of file From 6c89e3f8ecf5aadc7e34ff4c4c8eb2ca14c13211 Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Tue, 4 Jul 2023 07:15:59 -0400 Subject: [PATCH 3/5] Adjusted Timeout setter to throw an out of range exception. --- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index 1c7fcad3..e1077ca5 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -803,12 +803,12 @@ internal static HttpClient GetSharedHttpClient(string userAgent) public DecompressionMethods AutomaticDecompression { get; set; } /// - /// Gets or sets the timeout value in milliseconds. + /// Gets or sets the timeout value in milliseconds. Must be greater than zero. /// public int Timeout { get { return _timeout; } - set { if (value < 0) { _timeout = 0; } else { _timeout = value; } } + set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _timeout = value; } } } /// From 0c448c6ef5492dc883edc9222a0bc51964369e0f Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Tue, 4 Jul 2023 08:05:49 -0400 Subject: [PATCH 4/5] Allow for infinite timespan on the Timeout property. --- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index e1077ca5..f04579c6 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -808,7 +808,7 @@ internal static HttpClient GetSharedHttpClient(string userAgent) public int Timeout { get { return _timeout; } - set { if (value <= 0) { throw new ArgumentOutOfRangeException(); } else { _timeout = value; } } + set { if (value <= 0 && value != -1) { throw new ArgumentOutOfRangeException(); } else { _timeout = value; } } } /// From 058b50ee2a088dd751200feb846e10c058286e32 Mon Sep 17 00:00:00 2001 From: Nedim Basic Date: Tue, 4 Jul 2023 08:08:10 -0400 Subject: [PATCH 5/5] Added a comment. --- src/HtmlAgilityPack.Shared/HtmlWeb.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HtmlAgilityPack.Shared/HtmlWeb.cs b/src/HtmlAgilityPack.Shared/HtmlWeb.cs index f04579c6..eb29af86 100644 --- a/src/HtmlAgilityPack.Shared/HtmlWeb.cs +++ b/src/HtmlAgilityPack.Shared/HtmlWeb.cs @@ -803,7 +803,7 @@ internal static HttpClient GetSharedHttpClient(string userAgent) public DecompressionMethods AutomaticDecompression { get; set; } /// - /// Gets or sets the timeout value in milliseconds. Must be greater than zero. + /// Gets or sets the timeout value in milliseconds. Must be greater than zero. A value of -1 sets the timeout to be infinite. /// public int Timeout {