【问题标题】:How to post data with webview2如何使用 webview2 发布数据
【发布时间】:2020-12-20 10:25:56
【问题描述】:

我需要一个登录请求。我在 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();

我要做的是先登录用户,然后向他展示视图。

【问题讨论】:

标签: c# wpf webview2


【解决方案1】:

使用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);

【讨论】:

  • 如果您将postData 设置为null,则浏览器实际上不会导航,也不会抛出异常。
【解决方案2】:

好吧,当我给你那个链接时,这并不像我想象的那么容易。

首先,CoreWebView2.NavigateWithWebResourceRequest(CoreWebView2WebResourceRequest) 仅包含在WebView2pre-release 版本中。

其次,CoreWebView2WebResourceRequest 没有公共构造函数,因此您不能实际使用它(在WebResourceRequested 事件处理程序之外)。

这显然是一个“预发布”的东西,还没有准备好使用。

那么,你是做什么的?

好吧,我建议,你用的方法,我已经用了一段时间了:

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.

这应该让你登录。

【讨论】:

  • 实际上我得到了一条获取路线并通过了我的 api-token 所以我现在用它登录但是谢谢。我可能会构建一些 javascript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多