puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

HttpClient instead of WebClient

Open ronpathak opened this issue 2 years ago • 6 comments

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

ronpathak avatar Dec 17 '21 21:12 ronpathak

PRs are welcome :)

kblok avatar Dec 20 '21 14:12 kblok

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

ronpathak avatar Dec 20 '21 17:12 ronpathak

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.

Cheatoid avatar Jan 08 '22 07:01 Cheatoid

How important is the DownloadProgressChanged event? Because that is not directly supported by HttpClient.

campersau avatar Jan 15 '22 15:01 campersau

@campersau that's part of the public API

kblok avatar Jan 18 '22 13:01 kblok

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.

Cheatoid avatar Jan 20 '22 12:01 Cheatoid

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.

amazingalek avatar Feb 19 '23 15:02 amazingalek