How to detect navigation using the Webbrowser class and capture information with C#

Asked

Viewed 149 times

0

Hello, I’m doing an integration with the Free Market API, where I need to open a browser screen with the page for the user to allow access to my application’s API (as the documentation here). To do this, I intend to take advantage of the class System.Windows.Forms.WebBrowser that opens Internet Explorer on the page I need.

By the class master, I can use the event Navigated passing a method that would allow me to check from the browser URL if the page I passed as redirect_url is the one that was called (so I can get the authentication token) so:

public class Test
{
    public void requestCode()
    {
        var clientId = 123;                // algum clientId
        var redUrl = "https://www.foo.com" // minha url de redirect
        var url =
            $"https://auth.mercadolivre.com.br/authorization?"+
            $"response_type=code&" +
            $"client_id={clientId}&"+
            $"redirect_uri={redUrl}";
        try
        {
            using (var browser = new WebBrowser())
            {
                browser.Navigated += 
                    new WebBrowserNavigatedEventHandler(
                        requestAuthorizeOnNavigated
                    );
                browser.Navigate(url, Guid.NewGuid().ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    public void requestAuthorizeOnNavigated(
        object sender, 
        WebBrowserNavigatedEventArgs e
    )
    {
        Console.Write(sender.ToString(), e.ToString());
    }
}

When I run my code, I can open the free market page that generates the token (or that asks the user to authorize initially). The problem is that I’m not managing to detect navigation, even declaring the event within using...

What I need to do to be able to detect browsing (and read the URI that was reported in the last navigation) from this example?

  • 2

    If you use the event DocumentCompleted can get the URL from the parameter e who’s kind WebBrowserDocumentCompletedEventArgs.

  • I’ll see if I can use it this way... Thank you

1 answer

0


To solve my problem I had to reformulate my application so that it behaved like a webaplication and my application could make use of all the resources of solutions on the web because after ML authenticate it redirects the user request to the URL sent in the parameter redirect_uri which is described in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.