我想
e.Response
永远都是
null
因为
WebResourceRequested
事件在发送请求之前触发。您应该在eventhandler中创建响应。那不是你想要的。
那你是做什么的?嗯,新的
预发布
版本
WebView2
可能有,你想要的-a
WebResourceResponseReceived
事件当您从服务器收到响应时,会触发此操作
e、 响应
将可用。
要使用它:
-
安装/更新
Microsoft.WebView2
预发布版本
(最新版本)。
-
安装
金丝雀
Microsoft Edge的构建。
-
卸载
WebView2运行时-否则仍将使用。
现在,您可以使用如下代码(请注意新的事件名称):
using Microsoft.Web.WebView2.Core;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
webView21.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
}
private void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
if (e.Response != null && e.Response.Headers.Contains("date"))
{
int statusCode = e.Response.StatusCode;
string header = e.Response.Headers.GetHeader("date");
MessageBox.Show(header);
}
}
}
}
请注意
e.Response.Headers.GetHeader
如果标头不存在,将引发异常,请先检查它。