1

I wanna get a json from the response's body of this API:

// http://localhost:3000/api/auth/[token]
export default function Auth(request, response) {
    response.status(200).json({ token: request.query})
}

Trying the WebView.CoreWebView2.WebResourceResponseReceived event fires just once and the event arg's request Uri parameter is "http://localhost:3000/favicon.ico".

How can I get the response content?

What I did:

public partial class SignInUserControl : UserControl
{
    public SignInUserControl()
    {
        InitializeComponent();
        InitWebView();
    }
    async void InitWebView()
    {
        await WebView.EnsureCoreWebView2Async(null);
        WebView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
    }
    async void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
    {
        try
        {
            Stream stream = await e.Response.GetContentAsync();
            TextReader tr = new StreamReader(stream);
            string re = tr.ReadToEnd();
        }
        catch { }
    }
}

What I expect:

http://localhost:3000/api/auth/42sad87aWasFGAS
re = {"token":"42sad87aWasFGAS"} // From CoreWebView2_WebResourceResponseReceived method

ps: The WebViewer2 Control is working. So I don't think the problem is related to its initialization. working example

  • For CoreWebView2 initialization, the following may be helpful: https://stackoverflow.com/a/73846538/10024425 and https://stackoverflow.com/a/66501901/10024425 – Tu deschizi eu inchid Jan 17 '23 at 19:05
  • Thanks. But I don't think it's related to initialization. Take a look at the "ps" that I've added to the question. – Nelson Henrique Jan 17 '23 at 19:36
  • The initialization issue may or may not yet be apparent and you've not posted enough code to know if it's an issue for sure. The issue may be more apparent if one deletes the WebView2 _UserDataFolder_ between executions. Nonetheless, CoreWebView2 initialization needs to be performed properly before moving on to anything else. – Tu deschizi eu inchid Jan 17 '23 at 20:25
  • Ok. I've just updated with the SignIn User Control's full code. I'm gonna read these answers now. – Nelson Henrique Jan 17 '23 at 21:28
  • 1
    If you're getting the web response event handler called for the favicon but not the HTML document you may have a race. If you are setting the WebView2.Source property in XAML to start navigation of the WebView2 then navigation will be started during InitializeComponent but you only attach your WebResourceResponseReceived event handler later after that. You should ensure that you start your navigation after you set the WebResourceResponseReceived event handler. – David Risney Jan 19 '23 at 19:47

1 Answers1

0

The problem really was the WebView initialization. ‍♂️ Thanks to @user09938 and @david-risney

What did I do? I removed the Source property from the Xaml and made these changes:

public partial class SignInUserControl : UserControl
{
    public SignInUserControl()
    {
        InitializeComponent();
        InitwebView();
    }
    private void InitwebView()
    {
        WebView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
        WebView.EnsureCoreWebView2Async(null).GetAwaiter();
        WebView.Source = new Uri("http://localhost:3000/api/auth/NelsonHenrique");
    }
    private void WebView_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
    {
        WebView.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
    }
    private void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
    {
        var result = e.Response.GetContentAsync().GetAwaiter();
        result.OnCompleted(() =>
        {
            try
            {
                var res = result.GetResult();
                StreamReader reader = new StreamReader(res);
                string text = reader.ReadToEnd();
                // text: "{\"token\":\"NelsonHenrique\"}"
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        });
    }
}
  • Unfortunately, it's still not correct. Subscribing to`WebView.CoreWebView2InitializationCompleted` should occur BEFORE the call to `WebView.EnsureCoreWebView2Async()`. – Tu deschizi eu inchid Jan 22 '23 at 03:24