cefsharp如何拦截xhr请求获取响应体值?

时间:2017-02-27 23:09:42

标签: c# cefsharp

CefSharp是否能够拦截XHRHttpRequest并获取响应体值?如果是的话,我想知道它是如何完成的。

1 个答案:

答案 0 :(得分:0)

我为此创建了以下类,并从IRequestHandler.GetResourceResponseFilter返回了它的实例:

internal class ResponseSniffer : IResponseFilter
{
    private readonly Action<Stream> _streamHandler;

    public ResponseSniffer(Action<Stream> streamHandler)
    {
        _streamHandler = streamHandler;
    }

    public void Dispose()
    {
    }

    public bool InitFilter()
    {
        return true;
    }

    public FilterStatus Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
    {
        if (dataIn == null)
        {
            dataInRead = 0;
            dataOutWritten = 0;
            return FilterStatus.Error;
        }
        dataIn.CopyTo(dataOut);
        dataInRead = dataIn.Length;
        dataOutWritten = dataInRead;

        dataIn.Position = 0;
        _streamHandler(dataIn);
        return FilterStatus.Done;
    }
}