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 timeout of an HTTP request #502

Merged
merged 5 commits into from
Jul 6, 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
13 changes: 13 additions & 0 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Occurs after an HTTP request has been executed.
Expand Down Expand Up @@ -801,6 +802,15 @@ internal static HttpClient GetSharedHttpClient(string userAgent)
/// <value>The automatic decompression.</value>
public DecompressionMethods AutomaticDecompression { get; set; }

/// <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>
public int Timeout
{
get { return _timeout; }
set { if (value <= 0 && value != -1) { throw new ArgumentOutOfRangeException(); } else { _timeout = value; } }
}

/// <summary>
/// Gets or Sets a value indicating if document encoding must be automatically detected.
/// </summary>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down