Partilhar via


CoreWebView2WebResourceResponseView.GetContentAsync Method

Definition

Gets the response content stream asynchronously. The content failed to load.

public System.Threading.Tasks.Task<System.IO.Stream> GetContentAsync ();
member this.GetContentAsync : unit -> System.Threading.Tasks.Task<System.IO.Stream>
Public Function GetContentAsync () As Task(Of Stream)

Returns

Exceptions

The content failed to load.

Examples

ShowNextWebResponse = false;

CoreWebView2WebResourceRequest request = e.Request;
CoreWebView2WebResourceResponseView response = e.Response;

string caption = "Web Resource Response Received";
// Start with capacity 64 for minimum message size
StringBuilder messageBuilder = new StringBuilder(64);
string HttpMessageContentToString(System.IO.Stream content) => content == null ? "[null]" : "[data]";
void AppendHeaders(IEnumerable headers)
{
    foreach (var header in headers)
    {
        messageBuilder.AppendLine($"  {header}");
    }
}

// Request
messageBuilder.AppendLine("Request");
messageBuilder.AppendLine($"URI: {request.Uri}");
messageBuilder.AppendLine($"Method: {request.Method}");
messageBuilder.AppendLine("Headers:");
AppendHeaders(request.Headers);
messageBuilder.AppendLine($"Content: {HttpMessageContentToString(request.Content)}");
messageBuilder.AppendLine();

// Response
messageBuilder.AppendLine("Response");
messageBuilder.AppendLine($"Status: {response.StatusCode}");
messageBuilder.AppendLine($"Reason: {response.ReasonPhrase}");
messageBuilder.AppendLine("Headers:");
AppendHeaders(response.Headers);
try
{
    Stream content = await response.GetContentAsync();
    messageBuilder.AppendLine($"Content: {HttpMessageContentToString(content)}");
}
catch (System.Runtime.InteropServices.COMException)
{
    messageBuilder.AppendLine($"Content: [failed to load]");
}

MessageBox.Show(messageBuilder.ToString(), caption);

Remarks

A null stream means no content was found. Note content (if any) for redirect responses is ignored. This method returns null if content size is more than 123MB. If msWebView2EnableDownloadContentInWebResourceResponseReceived feature flag is disabled, for navigations that become downloads or if response is downloadable content type (e.g., application/octet-stream), this method also returns null. See DownloadStarting event to handle the response or enable the feature flag. The Stream object that is returned will be thread-safe and as reads will be blocking while waiting for data to be available, it is recommended to read from a background thread. If this method is being called again before a first call has completed, it will complete at the same time all prior calls do. If this method is being called after a first call has completed, it will return immediately (asynchronously).

Applies to