How to intercept HTTP request in WebView2

john zyd 421 Reputation points
2021-01-15T22:08:30.6+00:00

Hello:
I want to know if I can intercept HTTP request in WebView2. As in Puppeteer, I can intercept HTTP request.
For example, I launched MS Edge browser to visit: https://www.bing.com/
And I input “microsoft” in the search input box, then click search icon, then from network Tab, I can see there are many HTTP requests.
I want to intercept HTTP request which contains the string “https://www.bing.com/search?q=microsoft”
I can’t find any good example on how to do this, or if it is possible now?
Thanks,

Developer technologies Visual Studio Debugging
0 comments No comments
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 34,436 Reputation points Microsoft External Staff
    2021-01-20T03:37:04.36+00:00

    Hi @john zyd ,

    I think, this JS can return the results correctly by using Fetch API, and work in dev-tools, but fail when returning the result by using ExecuteScriptAsync() function in C#.

    58415-js.png

    This should be related to the encoding. As you can see the Remarks for ExecuteScriptAsync() function: “If the result is undefined, contains a reference cycle, or otherwise is not able to be encoded into JSON, the JSON null value is returned as the null string.”

    I suggest you can also start a new thread(send a feedback) here: GitHub MicrosoftEdge/WebView2Feedback – Issues.

    Best Regards,
    Tianyu

    • If the answer is helpful, please click "Accept Answer" and upvote it.
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. cheong00 3,486 Reputation points Volunteer Moderator
    2021-01-20T03:51:33.767+00:00

    You can just use any debugging proxy like Fiddler2. AFAIK WebView2 will use whatever proxy the Chromium based Edge configured to use.

    Alternatively see here on how to configure it with code (WPF version of WebView2 only, seems no way for WinForm version yet).

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.