Langue

CoreWebView2.NavigateWithWebResourceRequest Method

Definition

Navigates using a constructed CoreWebView2WebResourceRequest object.

public void NavigateWithWebResourceRequest(Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequest Request);
member this.NavigateWithWebResourceRequest : Microsoft.Web.WebView2.Core.CoreWebView2WebResourceRequest -> unit
Public Sub NavigateWithWebResourceRequest (Request As CoreWebView2WebResourceRequest)

Parameters

Request
CoreWebView2WebResourceRequest

The constructed web resource object to provide post data or additional request headers during navigation.

Examples

// Prepare post data as UTF-8 byte array and convert it to stream
// as required by the application/x-www-form-urlencoded Content-Type
var dialog = new TextInputDialog(
    title: "NavigateWithWebResourceRequest",
    description: "Specify post data to submit to https://www.w3schools.com/action_page.php.");
if (dialog.ShowDialog() == true)
{
    string postDataString = "input=" + dialog.Input.Text;
    UTF8Encoding utfEncoding = new UTF8Encoding();
    byte[] postData = utfEncoding.GetBytes(
        postDataString);
    MemoryStream postDataStream = new MemoryStream(postDataString.Length);
    postDataStream.Write(postData, 0, postData.Length);
    postDataStream.Seek(0, SeekOrigin.Begin);
    CoreWebView2WebResourceRequest webResourceRequest =
      WebViewEnvironment.CreateWebResourceRequest(
        "https://www.w3schools.com/action_page.php",
        "POST",
        postDataStream,
        "Content-Type: application/x-www-form-urlencoded\r\n");
    _iWebView2.CoreWebView2.NavigateWithWebResourceRequest(webResourceRequest);
}

Remarks

The headers in the CoreWebView2WebResourceRequest override headers added by WebView2 runtime except for Cookie headers. Method can only be either GET or POST. Provided post data will only be sent only if the method is POST and the uri scheme is HTTP(S).

The headers are sent with the initial request and with every redirect, including redirects to a different origin. They are also stored with the navigation history entry, so reload and back or forward navigations send them again. Do not include credentials, such as an Authorization header, unless you trust every server to which the navigation might be redirected. To control which origins receive a header, omit it from the CoreWebView2WebResourceRequest and add it for trusted origins in a WebResourceRequested event handler. Alternatively, use WebResourceRequested to remove the header when a redirect targets an untrusted origin.

Applies to