WebView2 代理 C++

问题描述

我在 GitHub 上找到了 this 线程,但似乎代码不是 C++:

WebView2 _webView2 = new WebView2();

CoreWebView2EnvironmentOptions options = new CoreWebView2EnvironmentOptions();

// Set a proxy pac for the browser   
//      options.AdditionalbrowserArguments = "--proxy-pac-url=http://myproxy.com/my.pac";

// Set the proxy for the browser
options.AdditionalbrowserArguments = "--proxy-server=\"foopy:99\"";

// Create the environment manually
CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(null,null,options);
await _webView2 .EnsureCoreWebView2Async(env);

所以我唯一要求的是提供通过 C++ 为 WebView2 设置代理的解决方案。


我有 ICoreWebView2 接口,但它没有 EnsureCoreWebView2Async 方法。另一方面,我有 CoreWebView2EnvironmentOptions 课。

解决方法

auto opt = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();

opt->put_AdditionalBrowserArguments(L"--proxy-server=\"SERVER\"");

CreateCoreWebView2EnvironmentWithOptions(nullptr,nullptr,opt.Get(),Microsoft::WRL::Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hwnd](HRESULT result,ICoreWebView2Environment* env) -> HRESULT {

...

}).Get());

而不是 SERVER 输入 ip 地址或其他内容。


我测试过,它有效,但似乎存在一个错误(或功能):您不能创建两个或多个具有不同运行参数的 webview。