Skip to content

Commit

Permalink
update source
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanMagnan committed Jan 14, 2022
1 parent def26b2 commit 127fe37
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/HtmlAgilityPack.Shared/HtmlAttributeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace HtmlAgilityPack
{
Expand Down Expand Up @@ -354,15 +355,21 @@ public void Remove(string name)
{
throw new ArgumentNullException("name");
}


List<int> listToRemove = new List<int>();
for (int i = 0; i < items.Count; i++)
{
HtmlAttribute att = items[i];
if (String.Equals(att.Name, name, StringComparison.OrdinalIgnoreCase))
{
RemoveAt(i);
listToRemove.Add(i);
}
}

foreach(var i in listToRemove.OrderByDescending(x => x))
{
RemoveAt(i);
}
}

/// <summary>
Expand Down
32 changes: 28 additions & 4 deletions src/HtmlAgilityPack.Shared/HtmlWeb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Concurrent;
#endif
#if FX40 || FX45
using System.Collections;
Expand Down Expand Up @@ -117,6 +118,21 @@ public partial class HtmlWeb

#region Static Members

#if FX45 || NETSTANDARD
internal static ConcurrentDictionary<string, HttpClient> SharedHttpClient = new ConcurrentDictionary<string, HttpClient>();


internal static HttpClient GetSharedHttpClient(string userAgent)
{
return SharedHttpClient.GetOrAdd(userAgent, x =>
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
return client;
});
}
#endif

//private static Dictionary<string, string> _mimeTypes;

//public static Dictionary<string, string> MimeTypes
Expand Down Expand Up @@ -2367,11 +2383,19 @@ public async Task<HtmlDocument> LoadFromWebAsync(Uri uri, Encoding encoding, Net
clientHandler.AllowAutoRedirect = false;
}

var client = new HttpClient(clientHandler);
HttpClient client;

if(credentials != null || CaptureRedirect)
{
client = new HttpClient(clientHandler);

//https://stackoverflow.com/questions/44076962/how-do-i-set-a-default-user-agent-on-an-httpclient
client.DefaultRequestHeaders.Add("User-Agent", this.UserAgent);

//https://stackoverflow.com/questions/44076962/how-do-i-set-a-default-user-agent-on-an-httpclient
client.DefaultRequestHeaders.Add("User-Agent", this.UserAgent);
}
else
{
client = GetSharedHttpClient(this.UserAgent);
}

var e = await client.GetAsync(uri, cancellationToken).ConfigureAwait(false);

Expand Down

0 comments on commit 127fe37

Please sign in to comment.