Skip to content

Commit

Permalink
Merge pull request #502 from basicn86/master
Browse files Browse the repository at this point in the history
Added ability to set the timeout of an HTTP request
  • Loading branch information
JonathanMagnan committed Jul 6, 2023
2 parents a22812c + 058b50e commit c12580b
Showing 1 changed file with 13 additions and 0 deletions.
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 static long SaveStream(Stream stream, string path, DateTime touchDate, i
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 static long SaveStream(Stream stream, string path, DateTime touchDate, i
using (var handler = new HttpClientHandler())
using (var client = new HttpClient(handler))
{
client.Timeout = TimeSpan.FromMilliseconds(Timeout);

if(CaptureRedirect)
{
handler.AllowAutoRedirect = false;
Expand Down

0 comments on commit c12580b

Please sign in to comment.