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

use HttpClient to replace WebClient #11542

Merged
merged 1 commit into from
Jun 21, 2024
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
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();
RussKie marked this conversation as resolved.
Show resolved Hide resolved
}
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