Skip to content

Commit

Permalink
use HttpClient to replace WebClient
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperk81 committed Jun 19, 2024
1 parent 5e1b5b5 commit c586555
Showing 1 changed file with 18 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public partial class PictureBox : Control, ISupportInitialize
!AppContext.TryGetSwitch("System.Windows.Forms.PictureBox.UseWebRequest", out bool useWebRequest)
|| useWebRequest;

private static readonly HttpClient s_httpClient = !LocalAppContextSwitches.ServicePointManagerCheckCrl ? new() :
new(new HttpClientHandler { CheckCertificateRevocationList = true });

/// <summary>
/// The type of border this control will have.
/// </summary>
Expand Down Expand Up @@ -464,23 +467,26 @@ public void Load()
try
{
DisposeImageStream();
if (UseWebRequest())
Uri uri = CalculateUri(_imageLocation);
if (uri.IsFile)
{
LoadImageViaWebClient();
_localImageStreamReader = new StreamReader(uri.LocalPath);
Image img = Image.FromStream(_localImageStreamReader.BaseStream);
InstallNewImage(img, ImageInstallationType.FromUrl);
}
else
else if (UseWebRequest())
{
Uri uri = CalculateUri(_imageLocation);
if (uri.IsFile)
// Run async operation synchronously to avoid blocking UI thread and potential deadlocks.
Task.Run(async () =>
{
_localImageStreamReader = new StreamReader(uri.LocalPath);
Image img = Image.FromStream(_localImageStreamReader.BaseStream);
_uriImageStream = await s_httpClient.GetStreamAsync(uri).ConfigureAwait(false);
Image img = Image.FromStream(_uriImageStream);
InstallNewImage(img, ImageInstallationType.FromUrl);
}
else
{
throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported);
}
}).GetAwaiter().GetResult();
}
else
{
throw new NotSupportedException(SR.PictureBoxRemoteLocationNotSupported);
}
}
catch
Expand All @@ -497,34 +503,6 @@ public void Load()
}
}

private void LoadImageViaWebClient()
{
Image img;
Uri uri = CalculateUri(_imageLocation!);
if (uri.IsFile)
{
_localImageStreamReader = new StreamReader(uri.LocalPath);
img = Image.FromStream(_localImageStreamReader.BaseStream);
}
else
{
if (LocalAppContextSwitches.ServicePointManagerCheckCrl)
{
#pragma warning disable SYSLIB0014 // Type or member is obsolete
ServicePointManager.CheckCertificateRevocationList = true;
#pragma warning restore SYSLIB0014
}

#pragma warning disable SYSLIB0014 // Type or member is obsolete
using WebClient webClient = new(); // lgtm[cs/webrequest-checkcertrevlist-disabled] - Having ServicePointManager.CheckCertificateRevocationList set to true has a slim chance of resulting in failure. We have an opt-out for this rare event.
#pragma warning restore SYSLIB0014 // Type or member is obsolete
_uriImageStream = webClient.OpenRead(uri.ToString());
img = Image.FromStream(_uriImageStream);
}

InstallNewImage(img, ImageInstallationType.FromUrl);
}

[SRCategory(nameof(SR.CatAsynchronous))]
[SRDescription(nameof(SR.PictureBoxLoad1Descr))]
public void Load(string url)
Expand Down

0 comments on commit c586555

Please sign in to comment.