Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post data with webview2

Tags:

c#

wpf

webview2

I need a post request for a login. I'm using the webview2 package in WPF and using it.

My code is this:

  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();

what i want to do is login the user in first then show him the view.

like image 663
Ali Avatar asked Aug 29 '26 18:08

Ali


1 Answers

Using WebView2 version 1.0.790-prelease (probably even earlier), you can create a CoreWebView2WebResourceRequest object using webView.CoreWebView2.Environment.CreateWebResourceRequest(). This can then be passed to NavigateWithWebResourceRequest.

So, for example:

var request = webView.CoreWebView2.Environment.CreateWebResourceRequest(navigateData.Url, 
              "POST", postData, "Content-Type: application/x-www-form-urlencoded");
webView.CoreWebView2.NavigateWithWebResourceRequest(request);
like image 108
Jack Miller Avatar answered Sep 01 '26 08:09

Jack Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!