如何使用 webview2 post 数据
How to post data with webview2
我需要 post 登录请求。我在 WPF 中使用 webview2 包并使用它。
我的代码是这样的:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("Sample_url"));
request.Content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", "123"),
});
// webview2 does not contain a defenition for 'NavigateWithHttpRequestMessage'
Browser.NavigateWithHttpRequestMessage();
我想做的是先让用户登录然后显示视图。
好吧,这并不像我给你的时候想的那么容易 link。
首先,CoreWebView2.NavigateWithWebResourceRequest(CoreWebView2WebResourceRequest)
仅包含在WebView2
的pre-release
版本中。
其次,CoreWebView2WebResourceRequest
没有 public 构造函数,因此您不能实际使用它(在 WebResourceRequested
事件处理程序之外)。
这显然是 'pre-release' 尚未准备好使用的东西。
那么,你是做什么的?
嗯,我建议,你用这个方法,我已经用了一段时间了:
1. Navigate to the login page, using the `Source` property.
2. Get the names of the `input` elements for 'email' and 'passwords'.
3. Construct some javascript to set the values of those 2 elements and perform a `click` on the button, that posts the form.
那应该让你登录。
使用 WebView2 version 1.0.790-prelease
(可能更早),您可以使用 webView.CoreWebView2.Environment.CreateWebResourceRequest()
创建一个 CoreWebView2WebResourceRequest
对象。
然后可以将其传递给 NavigateWithWebResourceRequest
.
因此,例如:
var request = webView.CoreWebView2.Environment.CreateWebResourceRequest(navigateData.Url,
"POST", postData, "Content-Type: application/x-www-form-urlencoded");
webView.CoreWebView2.NavigateWithWebResourceRequest(request);
我需要 post 登录请求。我在 WPF 中使用 webview2 包并使用它。
我的代码是这样的:
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("Sample_url"));
request.Content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", "123"),
});
// webview2 does not contain a defenition for 'NavigateWithHttpRequestMessage'
Browser.NavigateWithHttpRequestMessage();
我想做的是先让用户登录然后显示视图。
好吧,这并不像我给你的时候想的那么容易 link。
首先,CoreWebView2.NavigateWithWebResourceRequest(CoreWebView2WebResourceRequest)
仅包含在WebView2
的pre-release
版本中。
其次,CoreWebView2WebResourceRequest
没有 public 构造函数,因此您不能实际使用它(在 WebResourceRequested
事件处理程序之外)。
这显然是 'pre-release' 尚未准备好使用的东西。
那么,你是做什么的?
嗯,我建议,你用这个方法,我已经用了一段时间了:
1. Navigate to the login page, using the `Source` property.
2. Get the names of the `input` elements for 'email' and 'passwords'.
3. Construct some javascript to set the values of those 2 elements and perform a `click` on the button, that posts the form.
那应该让你登录。
使用 WebView2 version 1.0.790-prelease
(可能更早),您可以使用 webView.CoreWebView2.Environment.CreateWebResourceRequest()
创建一个 CoreWebView2WebResourceRequest
对象。
然后可以将其传递给 NavigateWithWebResourceRequest
.
因此,例如:
var request = webView.CoreWebView2.Environment.CreateWebResourceRequest(navigateData.Url,
"POST", postData, "Content-Type: application/x-www-form-urlencoded");
webView.CoreWebView2.NavigateWithWebResourceRequest(request);