WPF webbrowser's LoadCompleted event

后端 未结 3 1530
面向向阳花
面向向阳花 2021-01-21 12:26

When will the WPF webbrowser\'s LoadCompleted event fires? Is this event waits for any ajax calls to complete in the aspx page.

i have a wpf app in which a webbrowser co

3条回答
  •  天涯浪人
    2021-01-21 12:41

    I have been able to solve such issue.

    You will need some 3rd party assemblies :

    1. FiddlerCore: will act as a http proxy, embedded in your application
    2. Awesomium.net: a WebBrowser control, that works with the chromium engine. I choose this engine, because it allows to specify a proxy server just for the application.

    The idea, as you guess, is to create an in-memory proxy server, and redirect your web browser control to this proxy.

    Then, FiddlerCore publishes some events where you can analyse the request/response, especially the body.

    As it acts a proxy, all communications, including Ajax calls, Flash calls, etc, are routed by the proxy.

    If it can help, a small code that help me to prototype this behavior:

    App.cs :

    /// 
    /// Interaction logic for App.xaml
    /// 
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            BootStrap();
            base.OnStartup(e);
        }
    
        private void BootStrap()
        {
            SetupInternalProxy();
            SetupBrowser();
        }
    
        private static void SetupBrowser()
        {
            // We may be a new window in the same process.
            if (!WebCore.IsRunning)
            {
                // Setup WebCore with plugins enabled.
                WebCoreConfig config = new WebCoreConfig
                {
                    ProxyServer = "http://127.0.0.1:" + FiddlerApplication.oProxy.ListenPort.ToString(),
                    EnablePlugins = true,
                    SaveCacheAndCookies = true
                };
                WebCore.Initialize(config);
            }
            else
            {
                throw new InvalidOperationException("WebCore should be already running");
            }
        }
    
        private void SetupInternalProxy()
        {
            FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
            FiddlerApplication.Log.OnLogString += (o, s) => Debug.WriteLine(s);
    
            FiddlerCoreStartupFlags oFCSF = FiddlerCoreStartupFlags.Default;
            //this line is important as it will avoid changing the proxy for the whole system.
            oFCSF = (oFCSF & ~FiddlerCoreStartupFlags.RegisterAsSystemProxy);
    
            FiddlerApplication.Startup(
                0,
                oFCSF
                );
        }
    
        private void FiddlerApplication_AfterSessionComplete(Session oSession)
        {
            Debug.WriteLine(oSession.GetResponseBodyAsString());
        }
    }
    

    MainWindow.xaml :

    
        
    
            
    
        
    
    

    And finally, MainWindow.xaml.cs :

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            browser.LoadURL("http://google.fr");
        }
    }
    

    You will have to add some plumbing to this application, in order to route and analyse the requestion from the app to the business class, but that's beyond the scope of this question.

提交回复
热议问题