zoukankan      html  css  js  c++  java
  • cefsharp 拦截所有请求 解决chunked导致数据接收不完整的问题

    拦截请求需要实现两个接口

    1. IRequestHandler 对请求进行自定义处理
    2. IResponseFilter 对response进行处理
      cef官方文档

    实现了IRequestHandler 以后需要给浏览器绑定
    例:

    broswer.RequestHandler = new CEFRequestHandler();
    

    tips:

    1. 在request中的GetResourceResponseFilter方法进行检测
    public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
    {
        var url = new Uri(request.Url);
        if (url.AbsoluteUri.Contains("http://www.baidu.com"))
        {
            //自定义的IResponseFilter,这里是按照官方文档的示例
            var filter = CEFFilterManger.CreateFilter(request.Identifier.ToString());
            return filter;
        }
        return null;
    }
    

    IResponseFilter

    public abstract class CEFResponseHandle : IResponseFilter
        {
            public string URI { get; set; }
            public List<byte> dataAll = new List<byte>();
    
            public string DataStr
            {
                get
                {
                    return Encoding.UTF8.GetString(dataAll.ToArray());
                }
            }
    
           
            public void Dispose()
            {
            }
    
            public abstract void Invoke();
    
            public FilterStatus Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
            {
                try
                {
    
                    if (dataIn == null || dataIn.Length == 0)
                    {
                        dataInRead = 0;
                        dataOutWritten = 0;
    
                        //string resultResponse = System.Text.Encoding.UTF8.GetString(dataAll.ToArray());
    
                        //try
                        //{
                        //    JsonOper.ToObject<Dictionary<string, object>>(DataStr);
                        //}
                        //catch (Exception e)
                        //{
                        //    return FilterStatus.NeedMoreData;
                        //}
    
                        return FilterStatus.Done;
                    }
                    //解决Transfer-Encoding: chunked数据不全的问题
                    if (dataIn.Length > dataOut.Length)
                    {
                        var data = new byte[dataOut.Length];
                        dataIn.Seek(0, SeekOrigin.Begin);
                        dataIn.Read(data, 0, data.Length);
                        dataOut.Write(data, 0, data.Length);
    
                        dataInRead = dataOut.Length;
                        dataOutWritten = dataOut.Length;
    
                        dataAll.AddRange(data);
                    }
                    else
                    {
                        dataInRead = dataIn.Length;
                        dataOutWritten = Math.Min(dataInRead, dataOut.Length);
    
                        dataIn.CopyTo(dataOut);
                        dataIn.Seek(0, SeekOrigin.Begin);
                        byte[] bs = new byte[dataIn.Length];
                        dataIn.Read(bs, 0, bs.Length);
                        dataAll.AddRange(bs);
    
                        dataInRead = dataIn.Length;
                        dataOutWritten = dataIn.Length;
                    }
    
                    return FilterStatus.NeedMoreData;
                }
                catch (Exception ex)
                {
                    dataInRead = dataIn.Length;
                    dataOutWritten = dataIn.Length;
    
                    return FilterStatus.Done;
                }
            }
    
    
            public bool InitFilter()
            {
                return true;
            }
        }
    
  • 相关阅读:
    Redis 3.0.4 链表
    Redis 3.0.4 简单动态字符串(sds)
    4. 寻找两个有序数组的中位数
    redis主从同步异常
    redis重命名flushall和flushdb重启失败
    redis3.2 aof重写
    【转载】Redis 4.0 自动内存碎片整理(Active Defrag)源码分析
    [转]memcached对key和value的限制 memcached的key最大长度和Value最大长度
    LSM树(Log-Structured Merge Tree)存储引擎
    Linux使用详解(进阶篇)
  • 原文地址:https://www.cnblogs.com/ives/p/12768533.html
Copyright © 2011-2022 走看看