Hello:
I want to use WebView2 to get the response body when I visit one specific web site, like "https://www.bing.com".
I created one C# Windows Form App, and have the following code now:
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace GetResponse
{
public partial class Form1 : Form
{
private readonly WebView2 WebView21 = new();
private static readonly Button GetResponse = new();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.FixedDialog;
StartPosition = FormStartPosition.CenterScreen;
SuspendLayout();
MaximizeBox = true;
MinimizeBox = true;
BackColor = Color.White;
ForeColor = Color.Black;
Size = new Size(1800, 800);
Text = "GetContentAsync";
FormBorderStyle = FormBorderStyle.FixedDialog;
StartPosition = FormStartPosition.CenterScreen;
SuspendLayout();
WebView21.CreationProperties = null;
WebView21.Location = new Point(50, 100);
WebView21.Name = "WebView21";
WebView21.Size = new Size(1700, 700);
WebView21.TabIndex = 0;
WebView21.ZoomFactor = 1D;
WebView21.Visible = true;
Controls.Add(WebView21);
GetResponse.Name = "Response";
GetResponse.Font = new Font("Arial", 10);
GetResponse.Location = new Point(850, 30);
GetResponse.Size = new Size(100, 40);
GetResponse.Text = "Hide";
GetResponse.Click += new EventHandler(Response_Click);
GetResponse.Visible = true;
Controls.Add(GetResponse);
ResumeLayout();
}
private async void Response_Click(object sender, EventArgs e)
{
try
{
}
catch (TimeoutException ex)
{
Debug.Print("[Response_Click] EX:" + ex.Message);
}
}
private void WebView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
{
Debug.Print("NavigationCompleted");
}
private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
}
private void WebView21_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
Debug.Print("WebResourceResponseReceived");
Debug.Print("How to get response body? How to use CoreWebView2WebResourceResponseView.GetContentAsync()?");
}
private void Form1_Load(object sender, EventArgs e)
{
WebView21.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
WebView21.CoreWebView2InitializationCompleted +=
new EventHandler<CoreWebView2InitializationCompletedEventArgs>(WebView21_CoreWebView2InitializationCompleted);
}
}
}
But I can't find any code example for using CoreWebView2WebResourceResponseView.GetContentAsync.
In my program, I want to search Bing for some words, like "WebView2", and after I click on the button GetResponse, I want to show
the HTTP response body. Those Http request is something like "https://www.bing.com/search?q=webvew2"
By the way, I am using Visual Studio 2019 Version 16.10.1, the version of WebView2 run time is: 1.0.864.35
My OS is: Windows 10 Version 21H1 (OS Build 19043.1055)
Please advise!
