puppeteer-sharp
puppeteer-sharp copied to clipboard
HttpClient instead of WebClient
When using PuppeteerSharp with BlazorWASM, i get the following error
System.PlatformNotSupportedException: System.Net.WebClient is not supported on this platform. Use System.Net.Http.HttpClient instead. at System.Net.WebClient..ctor() at PuppeteerSharp.BrowserFetcher..ctor() in C:\projects\puppeteer-sharp\lib\PuppeteerSharp\BrowserFetcher.cs:line 53
Is there any plan to update Puppeteer to work use HttpClient instead of WebClient
PRs are welcome :)
Thanks Darío,
At the risk of giving away just how new i am to coding... What do i need to do to make the nuget package that i installed from the nuget manager work please?
Many thanks for any help you can provide - and apologies for landing you with my first github question... cheers
Ron
According to my quick GitHub search, there is only one place where WebClient
is being used: in BrowserFetcher.
I would take a bite and say it should be pretty easy to migrate from WebClient
to HttpClient
.
How important is the DownloadProgressChanged
event? Because that is not directly supported by HttpClient
.
@campersau that's part of the public API
that's part of the public API
Right, but is it that important to have that functionality? How many projects are actually using it?
It is possible to do "progress report" with HttpClient too, but it is slightly more complicated. The key parts being having to use HttpCompletionOption.ResponseHeadersRead
and ReadAsStreamAsync
, then read the incoming response/content in chunks.
For some example, see this question & the first answer.
The only change I would make is to use shared ArrayPool
for smaller memory impact (in CopyToAsync
extension method), and use 8192
as a buffer size (instead of 81920
).
In this case, I would also like to point out that HttpClient
should be initialized only once (a.k.a. singleton); see here for more details. But, using singleton also has one downside: DNS changes are not honored; see here for more details.
I tried porting to HttpClient, but unfortunately it wasn't the only blocker for using PuppeteerSharp with BlazorWASM. System.Diagnostics.Process is used for launching the browser, with or without Headless mode, and it's not supported either. I don't think supporting WASM will be easy.