Try using GetContentAsync like this:
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
{
//WebView21.CoreWebView2.WebResourceResponseReceived += WebView21_WebResourceResponseReceived;
}
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 Form1_Load(object sender, EventArgs e)
{
WebView21.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
WebView21.CoreWebView2InitializationCompleted +=
new EventHandler<CoreWebView2InitializationCompletedEventArgs>(WebView21_CoreWebView2InitializationCompleted);
}
private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
WebView21.CoreWebView2.WebResourceResponseReceived += WebView21_WebResourceResponseReceived;
}
private async void WebView21_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
Stream stream = await e.Response.GetContentAsync();
TextReader tr = new StreamReader(stream);
string re = tr.ReadToEnd();
}
}
Update(6/18):
When a webpage loads, there are many resources, such as bing, where there are more than 100 resources.
The loading of each resource will trigger the WebResourceResponseReceived event.
Some can get a response normally (status==200), while some can't (status == 204 no content).
When its status is 204, the current error will be triggered.
If you just don't want to encounter this exception, you can judge the response code as follows:
private async void WebView21_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
if (e.Response.StatusCode==200)
{
Stream stream = await e.Response.GetContentAsync();
TextReader tr = new StreamReader(stream);
string re = tr.ReadToEnd();
}
}
However, these resources include text, images and other types of resources, and it is not appropriate to convert all of them into text.
And it is not the overall response body but individual resources, so I think this method may not be suitable for your current needs.
This is why I suggested in the previous comment to use WebRequest to get the response.
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.