首页 > 解决方案 > 如何在不使用系统全局代理设置的情况下在 PuppeteerSharp 中使用代理

问题描述

由于一些限制,我必须使用代理来访问一个特定的网站。为此,我订阅了一项付费代理服务,并尝试访问该网站,它成功了。但是,要使用 Google Chrome 的代理服务,我必须从 Windows Edge 浏览器配置代理设置,因此代理设置会影响我 PC 中的所有网络浏览器,这是我不想要的;因为,除了那个特定的网站,我可以直接使用我的 ISP 访问所有其他网站,没有任何问题。我尝试了以下 C# 代码,看看我是否只能在底层 Chromium 会话期间使用代理设置访问“WhatIsMyIP”网站。

    private static readonly LaunchOptions puppeteer_launchOptions = new LaunchOptions
{
Headless = false,
IgnoreHTTPSErrors = true,
Args = new[] {
"--no-sandbox",
"--disable-infobars",
"--disable-setuid-sandbox",
"--ignore-certificate-errors",
$"--proxy-server='1.2.3.4:5678'",
},
};

static async Task Main()
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
_browser = await Puppeteer.LaunchAsync(puppeteer_launchOptions);
Page _page = await _browser.NewPageAsync();
await _page.SetViewportAsync((new ViewPortOptions { Width = 1920, Height = 938 }));
_page.DefaultNavigationTimeout = 100000;
await _page.GoToAsync("https://www.whatismyip.com/");
await _page.WaitForTimeoutAsync(2000);
Console.WriteLine("My Public IP is:");
}

但是,我的代码不起作用,我收到错误消息:PuppeteerSharp.NavigationException HResult=0x80131500 Message=net::ERR_NO_SUPPORTED_PROXIES at https://www.whatismyip.com/ at https://www.whatismyip.com/ Source=PuppeteerSharp StackTrace:在 PuppeteerSharp.FrameManager.d__42.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 ProxyWhatIsMyIP.Program.d__3.MoveNext() 在 C:\DualDataLiveBet\BetFairProxy\ProxyWhatIsMyIP\ProxyWhatIsMyIP\Program.cs:line 31

内部异常 1:NavigationException: net::ERR_NO_SUPPORTED_PROXIES 在https://www.whatismyip.com/

但是,如果我在 Microsoft Edge 浏览器中更改了全局代理设置,并在我的 C# 代码中删除了代理设置,它就可以工作了。以下是工作代码:

    using PuppeteerSharp;
using System; 
using System.Threading.Tasks;
namespace ProxyWhatIsMyIP 
{ 
class Program 
{ 
public static Browser _browser;
private static readonly LaunchOptions puppeteer_launchOptions = new LaunchOptions
{
    Headless = false,
    IgnoreHTTPSErrors = true,
    Args = new[] {
    "--no-sandbox",
    "--disable-infobars",
    "--disable-setuid-sandbox",
    "--ignore-certificate-errors",
    },
};
static async Task Main()
{
    await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
    _browser = await Puppeteer.LaunchAsync(puppeteer_launchOptions);
    Page _page = await _browser.NewPageAsync();
    await _page.SetViewportAsync((new ViewPortOptions { Width = 1920, Height = 938 }));
    _page.DefaultNavigationTimeout = 100000;
    await _page.GoToAsync("https://www.whatismyip.com/");
    await _page.WaitForTimeoutAsync(2000);
    Console.WriteLine("My Public IP is:");
}
}

但这不是我想要的。我只想在这个 Chromium 会话期间使用代理,但保持全局代理设置不变(不要在我的 PC 中使用任何代理。)请告知我该怎么做?顺便说一句,我正在使用 Visual Studio 2019 版本 16.3.2 和 PuppeteerSharp 1.20.0,目标是 .NET Core 3.0。我的电脑在应用了最新更新的 Windows 10(版本 1903)上运行。谢谢,

标签: c#google-chromeproxymicrosoft-edgepuppeteer-sharp

解决方案


在搜索其他人的答案时,我犯了一个愚蠢的错字。其实,这很简单。启动选项有一些错误的文本,删除 http:// 就足够了,因为我使用的是直接 IP 地址。所以正确的语法是:

rivate static readonly LaunchOptions puppeteer_launchOptions = new LaunchOptions 
{
Headless = false,
IgnoreHTTPSErrors = true,
    Args = new [] {
        "--proxy-server=1.2.3.4:5678",
        "--no-sandbox",
        "--disable-infobars",
        "--disable-setuid-sandbox",
        "--ignore-certificate-errors",
    },

};

顺便说一句, --disable-infobars 从 1.19.0 到 1.20.0 似乎都不起作用;我总是看到信息:Chrome 正在由自动化测试软件控制。我记得有人说自从 1.19.0 Google Chrome 改变了一些东西,所以 --disable-infobars 不起作用,但他也提供了一些如何禁用警告的示例,但他的方法不包括 PuppeteerSharp,包括 Puppeteer。现在,我还想不通。
希望我的问题可以帮助其他人。这只是一个错字,不是什么大问题!


推荐阅读