Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reponse of OnBeforeResourceLoad is always null #378

Open
WilliamQue opened this issue Jan 20, 2025 · 2 comments
Open

Reponse of OnBeforeResourceLoad is always null #378

WilliamQue opened this issue Jan 20, 2025 · 2 comments

Comments

@WilliamQue
Copy link

I want to catch all responses after clicking the login button by BeforeResourceLoad enent of webview, and the responses are always null, so I can't get the resonses' bodies.

So, is the following a right way?

    private void WebView_OnBeforeResourceLoad(ResourceHandler rh) {
        if (rh.Url.Contains("user/info")) {
            if (rh.Response != null) {
                using (StreamReader reader = new StreamReader(rh.Response)){
                    string text = reader.ReadToEnd();
                    Console.WriteLine(text);
                }
            }
        }
    }

If you have a little more time , please help me how to get cookies when visiting an url. online help of webview2 is detailed, but this has too little to to be mastered easily.

@joaompneves
Copy link
Collaborator

BeforeResourceLoad is an event triggered before the actual request is made to the target, thats why you see a null Response.
AFAIK you can't get the responses once you let them flow to CEF (the webview core).
In order to do that you need to perform the requests by yourself (eg: using a plain HttpClient) in the BeforeResourceLoad, copy the headers and request payload into the request and then waiting for the response. Once you get the response from the HttpClient you can read and manipulate it and then pass it to the webview using the RespondWith method of the event object.

@WilliamQue
Copy link
Author

@joaompneves Thank you for your reply.

I just want to get some json format responses when loging in a website, it's easy to submit username and password or scan QR code to log in, but not easy to prepare the payload with incomprehensible fields。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants