首页 > 解决方案 > WebView2 代理 C++

问题描述

我在 GitHub 上找到了这个线程,但似乎代码不是 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课。

标签: c++webview2

解决方案


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。


推荐阅读