Hello:
I can use WebView2 to get some 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.IO;
using System.Windows.Forms;
namespace GetResponse
{
public partial class Form1 : Form
{
private static 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
{
//WebView21.CoreWebView2.WebResourceResponseReceived += WebView21_WebResourceResponseReceived;
}
catch (TimeoutException ex)
{
Debug.Print("[Response_Click] EX:" + ex.Message);
}
}
private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
Debug.Print("WebView21_CoreWebView2InitializationCompleted");
WebView21.CoreWebView2.WebResourceResponseReceived += WebView21_WebResourceResponseReceived;
}
private async void WebView21_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
try
{
if (e.Response.StatusCode == 200)
{
Stream stream = await e.Response.GetContentAsync();
TextReader tr = new StreamReader(stream);
string re = tr.ReadToEnd();
string show_url = "Request URL =" + e.Request.Uri;
Debug.Print(show_url);
Debug.Print(re);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
Debug.Print("[WebResourceResponseReceived] EX:" + ex.Message);
}
}
private void WebView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
Debug.Print("NavigationCompleted");
}
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);
}
}
}
The above works for some responses received, but for my program, it is already enough.
However, in my program, I need to navigate to different URLs (about 10), and get only the first response body, then visit next URL.
So I modified my code, so the Form1_Load function looks like this:
private async void Form1_Load(object sender, EventArgs e)
{
WebView21.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
await WebView21.EnsureCoreWebView2Async(null);
WebView21.CoreWebView2.Navigate("https://www.google.com/");
WebView21.CoreWebView2InitializationCompleted +=
new EventHandler<CoreWebView2InitializationCompletedEventArgs>(WebView21_CoreWebView2InitializationCompleted);
}
But the code didn't work, as when using CoreWebView2.Navigate, the WebView21.CoreWebView2InitializationCompleted simply did NOT work.
Any suggestions?