CEF3:拦截http request请求和response响应(包括ajax请求和响应也能拦截到)

Eliza ·
更新时间:2024-09-20
· 804 次阅读

文章目录前言思路代码 前言

笔者在项目开发中有需求,需要拦截 js中 发起的 http 请求和响应数据 写到文件中,方便给开发人员或者测试人员查看。笔者拿到这个需求第一反应是,cef肯定有这种接口可供我们使用,所以肯定能实现咯。这里笔者用的是cef2623版本

思路

笔者百度了一下 大致可以在 CefRequestHandler的回调函数可以得到一些东西,一开始 笔者找到的最相近的方法是 下面这个函数OnResourceLoadComplete,既有request 又有response

/// // Called on the IO thread when a resource load has completed. |request| and // |response| represent the request and response respectively and cannot be // modified in this callback. |status| indicates the load completion status. // |received_content_length| is the number of response bytes actually read. /// /*--cef()--*/ virtual void OnResourceLoadComplete(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, CefRefPtr response, URLRequestStatus status, int64 received_content_length) {}

结果空欢喜一场。我们可以通过request和response指针能 拿到请求头和请求体 和 响应头 响应状态码等 但就是得不到 响应体!

所以 百度不行,当然谷歌了呀。虽然没有得到具体代码提示,但是提示说在 示例程序中有,恍然大悟了 应该回归初心 ,有什么需求或者问题 都可以看cef示例工程。在cefclient有拦截request请求和响应的例子!

原创文章 224获赞 518访问量 78万+ 关注 他的留言板 展开阅读全文
作者:李四老师



cef HTTP request response AJAX

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章