1

How can I use dotnetbrowser doing the following in a winform application?

  • Create a listener that listen for callbacks to a specific redirect url.
  • Open url in dotnetbrowser. The url makes the callback to the redirect url in another thread
  • The listener catches the response from the callback.

I can do this with an ordinary webbrowser, but I would like it to be silent. That's why I try to use dotnetbrowser instead.

Is dotNetBrowser a good choice for this, or is there a better option?

This is from my test code with a non silent webbrowser. First I create a listener that listen to a redirectUri:

        var listener = new HttpListener();
        listener.Prefixes.Add(redirectURI);
        listener.Start();

Then I start the url in a webbrowser:

        Process p = Process.Start(url);

The started url will send a callback to the redirectUri. The listener will get it.

        var context = await listener.GetContextAsync(); ;
        string formData = string.Empty;
        using (var body = context.Request.InputStream)
        {
            using (var reader = new System.IO.StreamReader(body, context.Request.ContentEncoding))
            {
                formData = reader.ReadToEnd();
            }
        }
        listener.Close();
Mike
  • 59
  • 3

1 Answers1

2

I found a solution with help from dotnetbrowser support site.

This is the winform constructor in my new test project:

    public Form1()
    {
        webView = new BrowserView() { Dock = DockStyle.Fill };
        Task.Run(() =>
        {
            engine = EngineFactory.Create(new EngineOptions.Builder
            {
                RenderingMode = RenderingMode.HardwareAccelerated,
                LicenseKey = "your license key here"
            }
            .Build());
            browser = engine.CreateBrowser();
        })
        .ContinueWith(t =>
        {
            webView.InitializeFrom(browser);

            var listener = new HttpListener();
            listener.Prefixes.Add(redirectURI);
            listener.Start();

            browser.Navigation.LoadUrl(url);

            var context = listener.GetContextAsync().GetAwaiter().GetResult();
            //Get data from redirectUri. You find this code from test example above, but not really relevant for the problem.
            var formData = GetRequestPostData(context.Request);
            listener.Close();

        }, TaskScheduler.FromCurrentSynchronizationContext());

        InitializeComponent();
        FormClosing += Form1_FormClosing;
        Controls.Add(webView);

        this.Visible = false;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        browser?.Dispose();
        engine?.Dispose();
    }
Mike
  • 59
  • 3