3

I know this is possible using Puppeteer in js, but I'm wondering if anyone has figured out how to proxy on a page level in PuppeteerSharp (different proxies for different tabs)?.

it seems I can catch the request, but I'm not sure how to adjust the proxy.

page.SetRequestInterceptionAsync(true).Wait();                     
page.Request += (s, ev) =>
{
    // what to do?                            
}

Edit

I am aware that I can set the proxy at the browser level like so;

var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = false,
    Args = new[] { "--proxy-server=host:port" }
});

var page = await browser.NewPageAsync();
await page.AuthenticateAsync(new Credentials() { Username = "username", Password = "password" });

But this is not what I'm trying to do. I'm trying to set the proxy for each page within a single browser instance. I want to test lots of proxies so spawning a new instance of the browser just to set the proxy is too much overhead.

clamchoda
  • 4,411
  • 2
  • 36
  • 74

1 Answers1

0

You can use different browser instances for each logical instances. I mean instead of trying to set different proxy for each page/tab with different proxy just create new browser instance and set proxy via launch args.

If this solution doesn't fit your needs, check this question. There is library for NodeJS which give ability to use different proxy per each page/tab. You can check that library source code and implement same things inside your C# application.

That library is using very simple method. Instead of sending requests via puppeter's browser/page library send request via nodejs http tools. It can be done by using method page.setRequestInterception. So library intercept each request from page, after that gather data and send request via http tools. I used C# a long time ago. So maybe I am wrong, but you can try to use HttpWebRequest or something similar. After you get result you should use method request.respond and pass response results there. In this way you can put any kind of proxy inside your application. Check here code of library.

Jakeroid
  • 205
  • 2
  • 11