ICoreWebView2WebResourceRequest for C#

john zyd 421 Reputation points
2021-04-04T10:07:42.48+00:00

Hello:
I can see C++ version of WebView2 now has ICoreWebView2WebResourceRequest interface.
Since I want to use WebView2 to intercept some http requests and modify some headers, but I can't use C++ now, as all my projects are now in C#.
Is there any code example to use ICoreWebView2WebResourceRequest for C#?
Or, if not yet, do you have plan to port C++ WebView2 to C# in the near future?
PS: My OS: Windows 10 (20H2), IDE: Visual Studio 2019 Version 16.9.3.
Thanks,

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,304 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 86,986 Reputation points
    2021-04-04T16:54:22.83+00:00

    [Cannot add post in comments with code]

    A test with Bing.com =>

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //this.Load += new System.EventHandler(this.Form1_Load); // Added in Designer
        }
    
        private bool bInitialized = false;
    
        private async void button1_Click(object sender, EventArgs e)
        {
            if (bInitialized)
            {
                Microsoft.Web.WebView2.Core.CoreWebView2Environment webView2Environment = null;
                string sTempWebCacheDir = System.IO.Path.GetTempPath();
                webView2Environment = await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(null, sTempWebCacheDir, null);
    
                //string sHeaders = "accept: application/json";
                System.IO.Stream stm = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(""));
                //Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequest wrr = webView2Environment.CreateWebResourceRequest("https://httpbin.org/post", "POST", stm, sHeaders);
    
                string sHeaders = "Accept-Encoding gzip, deflate, br";
    
                Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequest wrr = webView2Environment.CreateWebResourceRequest("https://www.bing.com", "GET", stm, sHeaders);
                Microsoft.Web.WebView2.Core.CoreWebView2 cwv2 = webView21.CoreWebView2;
                cwv2.NavigateWithWebResourceRequest(wrr);
            }
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            webView21.Source = new System.Uri("about:blank", System.UriKind.Absolute);
            webView21.CoreWebView2InitializationCompleted += new System.EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs>(webView21_CoreWebView2InitializationCompleted);
         }
    
        private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            bInitialized = true;
            webView21.CoreWebView2.NavigationCompleted += webView21_NavigationCompleted;
            webView21.CoreWebView2.AddWebResourceRequestedFilter("*", Microsoft.Web.WebView2.Core.CoreWebView2WebResourceContext.All);            
            webView21.CoreWebView2.WebResourceRequested += webView21_WebResourceRequested;
            webView21.CoreWebView2.WebResourceResponseReceived += webView21_WebResourceResponseReceived;
        }
    
        private void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {
            System.Diagnostics.Debug.Print("NavigationCompleted");
        }
    
    
        private void webView21_WebResourceRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequestedEventArgs e)
        {
            System.Diagnostics.Debug.Print("WebResourceRequested");
            System.Diagnostics.Debug.Print(e.Request.Uri);
            // Add other Debug.Print...
        }
    
        private void webView21_WebResourceResponseReceived(object sender, Microsoft.Web.WebView2.Core.CoreWebView2WebResourceResponseReceivedEventArgs e)
        {
            System.Diagnostics.Debug.Print("WebResourceResponseReceived");
           // Add other Debug.Print...
        }
    }
    

2 additional answers

Sort by: Most helpful
  1. Castorix31 86,986 Reputation points
    2021-04-04T13:33:56.877+00:00

    This basic test works for me (with a WebView2 control named webView21 and an "async void button1_Click" (for the await)) on Windows 10 1909,
    Microsoft.Web.WebView2.1.0.824-prerelease =>

    Microsoft.Web.WebView2.Core.CoreWebView2Environment webView2Environment = null;
    string sTempWebCacheDir = System.IO.Path.GetTempPath();
    webView2Environment = await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(null, sTempWebCacheDir, null);
    
    string sHeaders = "accept: application/json";
    System.IO.Stream stm = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(""));
    Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequest wrr = webView2Environment.CreateWebResourceRequest("https://httpbin.org/post", "POST", stm, sHeaders);
    
    Microsoft.Web.WebView2.Core.CoreWebView2 cwv2 = webView21.CoreWebView2;
    cwv2.NavigateWithWebResourceRequest(wrr);
    
    0 comments No comments

  2. john zyd 421 Reputation points
    2021-04-04T14:12:30.547+00:00

    Hello:
    I posted a rather long answer to this question, but I can't see it even I tried to post it 3 times.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.