是否有可能拦截WPF浏览器控件中接收到的内容? [英] Is it possible to intercept the content received within a wpf browser control?

查看:139
本文介绍了是否有可能拦截WPF浏览器控件中接收到的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebBrowser控件WPF应用程序。

I have a WPF application with a WebBrowser control.

我想拦截和跟踪浏览器控制发出的http请求。

I'd like to intercept and trace the http request issued by the browser control.

我不希望修改的内容。我只想要执行时加载的URL是匹配特定模式的一些规则。特别是,我有一些Ajax调用所返回的填充Web控件的数据。

I don't want to modify the content. I only want to perform some rules when loaded urls are matching a specific pattern. Especially, I have some ajax call that are returning data that populate the web controls.

我要抓住这个数据在容器应用程序也采取行动。是否可以?怎么样?

I want to capture this data to act also in the container application. Is it possible? How?

该conrtol有LoadCompleted事件,但它只是在Source属性解雇的URI代表特定的,而不是subressourceS。

The conrtol have a LoadCompleted event, but it's only fired for the uri specifid in the Source property, not subressourceS.

推荐答案

重复,这里是解决方案:

我已经能够解决这样的问题。

I have been able to solve such issue.

您将需要一些第三方组件:

You will need some 3rd party assemblies :


  1. FiddlerCore :将作为一个HTTP代理,内嵌在应用程序中

  2. Awesomium.net :WebBrowser控件,与铬引擎的工作。我选择这款发动机,因为它允许指定代理服务器只为应用程序。

  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.

然后,FiddlerCore发布一些事件,你可以分析请求/响应,尤其是身体。

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

由于它充当一个代理,所有通信,包括Ajax调用,调用闪存等,都是由代理进行路由。

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

如果它可以帮助,小code,帮助我为原型此行为:

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

App.cs:

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
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:

MainWindow.xaml :

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1" 
        xmlns:Custom="http://schemas.awesomium.com/winfx" 
        x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="MainWindow_Loaded">
    <Grid>

        <Custom:WebControl Name="browser"/>

    </Grid>
</Window>

最后,MainWindow.xaml.cs:

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.

这篇关于是否有可能拦截WPF浏览器控件中接收到的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆