热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:WinForm窗体程序中使用CefSharp获取加载后的资源截取request参数拦截response数据注入jquery文件和js代码拦截response数据

篇首语:本文由编程笔记#小编为大家整理,主要介绍了WinForm窗体程序中使用CefSharp获取加载后的资源截取request参数拦截response数据注入jquery文件和js代码-拦截r

篇首语:本文由编程笔记#小编为大家整理,主要介绍了WinForm窗体程序中使用CefSharp获取加载后的资源截取request参数拦截response数据注入jquery文件和js代码-拦截response数据相关的知识,希望对你有一定的参考价值。




WinForm窗体程序中使用CefSharp获取加载后的资源、截取request参数、拦截response数据、注入jquery文件和js代码

源码地址:源代码csdn  或者底部qq问我要

五、cefsharp拦截response数据

与四类似,response数据也是要继承接口 IResourceRequestHandler,实现其方法,从方法中获取

1、添加类ResourceRequestHandler,继承IResourceRequestHandler,实现各种方法


public class ResourceRequestHandler : IResourceRequestHandler
{
///


/// Called on the CEF IO thread before a resource request is loaded. To optionally filter COOKIEs for the request return a
/// object.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// To optionally filter COOKIEs for the request return a ICOOKIEAccessFilter instance otherwise return null.
ICOOKIEAccessFilter IResourceRequestHandler.GetCOOKIEAccessFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return GetCOOKIEAccessFilter(chromiumWebBrowser, browser, frame, request);
}
///
/// Called on the CEF IO thread before a resource request is loaded. To optionally filter COOKIEs for the request return a
/// object.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// To optionally filter COOKIEs for the request return a ICOOKIEAccessFilter instance otherwise return null.
protected virtual ICOOKIEAccessFilter GetCOOKIEAccessFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return null;
}
///
/// Called on the CEF IO thread before a resource is loaded. To specify a handler for the resource return a
/// object.
///

/// The browser UI control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// To allow the resource to load using the default network loader return null otherwise return an instance of
/// with a valid stream.
///

IResourceHandler IResourceRequestHandler.GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return GetResourceHandler(chromiumWebBrowser, browser, frame, request);
}
///
/// Called on the CEF IO thread before a resource is loaded. To specify a handler for the resource return a
/// object.
///

/// The browser UI control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// To allow the resource to load using the default network loader return null otherwise return an instance of
/// with a valid stream.
///

protected virtual IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return null;
}
/// Called on the CEF IO thread to optionally filter resource response content.
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// Return an IResponseFilter to intercept this response, otherwise return null.
IResponseFilter IResourceRequestHandler.GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return GetResourceResponseFilter(chromiumWebBrowser, browser, frame, request, response);
}
/// Called on the CEF IO thread to optionally filter resource response content.
/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// Return an IResponseFilter to intercept this response, otherwise return null.
protected virtual IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return null;
}
///
/// Called on the CEF IO thread before a resource request is loaded. To redirect or change the resource load optionally modify
/// . Modification of the request URL will be treated as a redirect.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// Callback interface used for asynchronous continuation of url requests.
///
/// Return to continue the request immediately. Return
/// and call or
/// at a later time to continue or the cancel the request asynchronously. Return
/// to cancel the request immediately.
///

CefReturnValue IResourceRequestHandler.OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
return OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
}
///
/// Called on the CEF IO thread before a resource request is loaded. To redirect or change the resource load optionally modify
/// . Modification of the request URL will be treated as a redirect.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - can be modified in this callback.
/// Callback interface used for asynchronous continuation of url requests.
///
/// Return to continue the request immediately. Return
/// and call or
/// at a later time to continue or the cancel the request asynchronously. Return
/// to cancel the request immediately.
///

protected virtual CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
return CefReturnValue.Continue;
}
///
/// Called on the CEF UI thread to handle requests for URLs with an unknown protocol component. SECURITY WARNING: YOU SHOULD USE
/// THIS METHOD TO ENFORCE RESTRICTIONS BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// return to true to attempt execution via the registered OS protocol handler, if any. Otherwise return false.
///

bool IResourceRequestHandler.OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return OnProtocolExecution(chromiumWebBrowser, browser, frame, request);
}
///
/// Called on the CEF UI thread to handle requests for URLs with an unknown protocol component. SECURITY WARNING: YOU SHOULD USE
/// THIS METHOD TO ENFORCE RESTRICTIONS BASED ON SCHEME, HOST OR OTHER URL ANALYSIS BEFORE ALLOWING OS EXECUTION.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
///
/// return to true to attempt execution via the registered OS protocol handler, if any. Otherwise return false.
///

protected virtual bool OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return false;
}
///
/// Called on the CEF IO thread when a resource load has completed. This method will be called for all requests, including
/// requests that are aborted due to CEF shutdown or destruction of the associated browser. In cases where the associated browser
/// is destroyed this callback may arrive after the callback for that browser. The
/// method can be used to test for this situation, and care
/// should be taken not to call or methods that modify state (like LoadURL,
/// SendProcessMessage, etc.) if the frame is invalid.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// indicates the load completion status.
/// is the number of response bytes actually read.
void IResourceRequestHandler.OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
OnResourceLoadComplete(chromiumWebBrowser, browser, frame, request, response, status, receivedContentLength);
}
///
/// Called on the CEF IO thread when a resource load has completed. This method will be called for all requests, including
/// requests that are aborted due to CEF shutdown or destruction of the associated browser. In cases where the associated browser
/// is destroyed this callback may arrive after the callback for that browser. The
/// method can be used to test for this situation, and care
/// should be taken not to call or methods that modify state (like LoadURL,
/// SendProcessMessage, etc.) if the frame is invalid.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// indicates the load completion status.
/// is the number of response bytes actually read.
protected virtual void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
}
///
/// Called on the CEF IO thread when a resource load is redirected. The parameter will contain the old
/// URL and other request-related information. The parameter will contain the response that resulted
/// in the redirect. The parameter will contain the new URL and can be changed if desired.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// [in,out] the new URL and can be changed if desired.
void IResourceRequestHandler.OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
OnResourceRedirect(chromiumWebBrowser, browser, frame, request, response, ref newUrl);
}
///
/// Called on the CEF IO thread when a resource load is redirected. The parameter will contain the old
/// URL and other request-related information. The parameter will contain the response that resulted
/// in the redirect. The parameter will contain the new URL and can be changed if desired.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object - cannot be modified in this callback.
/// the response object - cannot be modified in this callback.
/// [in,out] the new URL and can be changed if desired.
protected virtual void OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
}
///
/// Called on the CEF IO thread when a resource response is received. To allow the resource load to proceed without modification
/// return false. To redirect or retry the resource load optionally modify and return true.
/// Modification of the request URL will be treated as a redirect. Requests handled using the default network loader cannot be
/// redirected in this callback.
///
/// WARNING: Redirecting using this method is deprecated. Use OnBeforeResourceLoad or GetResourceHandler to perform redirects.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object.
/// the response object - cannot be modified in this callback.
///
/// To allow the resource load to proceed without modification return false. To redirect or retry the resource load optionally
/// modify and return true. Modification of the request URL will be treated as a redirect. Requests
/// handled using the default network loader cannot be redirected in this callback.
///

bool IResourceRequestHandler.OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return OnResourceResponse(chromiumWebBrowser, browser, frame, request, response);
}
///
/// Called on the CEF IO thread when a resource response is received. To allow the resource load to proceed without modification
/// return false. To redirect or retry the resource load optionally modify and return true.
/// Modification of the request URL will be treated as a redirect. Requests handled using the default network loader cannot be
/// redirected in this callback.
///
/// WARNING: Redirecting using this method is deprecated. Use OnBeforeResourceLoad or GetResourceHandler to perform redirects.
///

/// The ChromiumWebBrowser control.
/// the browser object - may be null if originating from ServiceWorker or CefURLRequest.
/// the frame object - may be null if originating from ServiceWorker or CefURLRequest.
/// the request object.
/// the response object - cannot be modified in this callback.
///
/// To allow the resource load to proceed without modification return false. To redirect or retry the resource load optionally
/// modify and return true. Modification of the request URL will be treated as a redirect. Requests
/// handled using the default network loader cannot be redirected in this callback.
///

protected virtual bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
}
///
/// Called when the unamanged resource is freed.
/// Unmanaged resources are ref counted and freed when
/// the last reference is released, this works differently
/// to .Net garbage collection.
///

protected virtual void Dispose()
{
}
void IDisposable.Dispose()
{
Dispose();
}
}

2、要从Resource Filter中获取数据,先添加两个类FilterManager、TestJsonFilter,合并在一个文件FilterManager中,用来获取数据


public class TestJsonFilter : IResponseFilter
{
public List DataAll = new List();
public FilterStatus Filter(System.IO.Stream dataIn, out long dataInRead, System.IO.Stream dataOut, out long dataOutWritten)
{
try
{
if (dataIn == null || dataIn.Length == 0)
{
dataInRead = 0;
dataOutWritten = 0;
return FilterStatus.Done;
}
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;
}
public void Dispose()
{
}
}
public class FilterManager
{
private static Dictionary dataList = new Dictionary();
public static IResponseFilter CreateFilter(string guid)
{
lock (dataList)
{
var filter = new TestJsonFilter();
dataList.Add(guid, filter);
return filter;
}
}
public static IResponseFilter GetFileter(string guid)
{
lock (dataList)
{
return dataList[guid];
}
}
}

 

3、新建类WinFormResourceRequestHandler,继承ResourceRequestHandler类,重写覆盖父类方法GetResourceResponseFilter、OnResourceLoadComplete


public class WinFormResourceRequestHandler : ResourceRequestHandler
{
protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
var filter = FilterManager.CreateFilter(request.Identifier.ToString());
return filter;
}
protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
if (request.Url.ToLower().Contains("login".ToLower()))
{
var filter = FilterManager.GetFileter(request.Identifier.ToString()) as TestJsonFilter;
ASCIIEncoding encoding
= new ASCIIEncoding();
//这里截获返回的数据
var data = encoding.GetString(filter.DataAll.ToArray());
}
}
}

此处也只拦截login登录返回的数据。也可以匹配别的页面请求

4、要修改IRequestHandler的实现类WinFormsRequestHandler中GetResourceRequestHandler方法,return null,改为return WinFormResourceRequestHandler,来调用请求后资源加载前的处理方法。


public class WinFormsRequestHandler : RequestHandler
{
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
//NOTE: In most cases you examine the request.Url and only handle requests you are interested in
if (request.Url.ToLower().Contains("login".ToLower()))
{
using (var postData = request.PostData)
{
if (postData != null)
{
var elements = postData.Elements;
var charSet = request.GetCharSet();
foreach (var element in elements)
{
if (element.Type == PostDataElementType.Bytes)
{
var body = element.GetBody(charSet);
}
}
}
}
}
return new WinFormResourceRequestHandler();
}
}

5、此时就可以调试运行,拦截获取到login请求后的的数据

 



推荐阅读
  • 本文探讨了如何利用 jQuery 的 JSONP 技术实现跨域调用外部 Web 服务。通过详细解析 JSONP 的工作原理及其在 jQuery 中的应用,本文提供了实用的代码示例和最佳实践,帮助开发者解决跨域请求中的常见问题。 ... [详细]
  • Web开发框架概览:Java与JavaScript技术及框架综述
    Web开发涉及服务器端和客户端的协同工作。在服务器端,Java是一种优秀的编程语言,适用于构建各种功能模块,如通过Servlet实现特定服务。客户端则主要依赖HTML进行内容展示,同时借助JavaScript增强交互性和动态效果。此外,现代Web开发还广泛使用各种框架和库,如Spring Boot、React和Vue.js,以提高开发效率和应用性能。 ... [详细]
  • 网站访问全流程解析
    本文详细介绍了从用户在浏览器中输入一个域名(如www.yy.com)到页面完全展示的整个过程,包括DNS解析、TCP连接、请求响应等多个步骤。 ... [详细]
  • 在JavaWeb开发中,文件上传是一个常见的需求。无论是通过表单还是其他方式上传文件,都必须使用POST请求。前端部分通常采用HTML表单来实现文件选择和提交功能。后端则利用Apache Commons FileUpload库来处理上传的文件,该库提供了强大的文件解析和存储能力,能够高效地处理各种文件类型。此外,为了提高系统的安全性和稳定性,还需要对上传文件的大小、格式等进行严格的校验和限制。 ... [详细]
  • 本文详细探讨了 jQuery 中 `ajaxSubmit` 方法的使用技巧及其应用场景。首先,介绍了如何正确引入必要的脚本文件,如 `jquery.form.js` 和 `jquery-1.8.0.min.js`。接着,通过具体示例展示了如何利用 `ajaxSubmit` 方法实现表单的异步提交,包括数据的发送、接收和处理。此外,还讨论了该方法在不同场景下的应用,如文件上传、表单验证和动态更新页面内容等,提供了丰富的代码示例和最佳实践建议。 ... [详细]
  • 《精通 jQuery》第六章:深入解析与实战应用
    《精通 jQuery》第六章:深入解析与实战应用本章详细探讨了 Ajax 技术的核心机制及其实际应用。Ajax 通过 XMLHttpRequest 对象实现客户端与服务器之间的异步数据交换,从而在不重新加载整个页面的情况下更新部分内容。这种技术不仅提升了用户体验,还提高了应用的响应速度和效率。此外,本章还介绍了如何利用 jQuery 简化 Ajax 操作,并提供了多个实战案例,帮助读者更好地理解和掌握这一重要技术。 ... [详细]
  • 用阿里云的免费 SSL 证书让网站从 HTTP 换成 HTTPS
    HTTP协议是不加密传输数据的,也就是用户跟你的网站之间传递数据有可能在途中被截获,破解传递的真实内容,所以使用不加密的HTTP的网站是不 ... [详细]
  • Spring Boot 中配置全局文件上传路径并实现文件上传功能
    本文介绍如何在 Spring Boot 项目中配置全局文件上传路径,并通过读取配置项实现文件上传功能。通过这种方式,可以更好地管理和维护文件路径。 ... [详细]
  • DVWA学习笔记系列:深入理解CSRF攻击机制
    DVWA学习笔记系列:深入理解CSRF攻击机制 ... [详细]
  • 深入解析Struts、Spring与Hibernate三大框架的面试要点与技巧 ... [详细]
  • 基于Java和SSM框架的志愿者管理平台源代码分析与实现
    本研究针对基于Java和SSM框架的志愿者管理平台进行了详细的源代码分析与实现。该平台属于Java Web项目,采用Java EE技术栈,并结合了Spring、Spring MVC和MyBatis三大核心框架(非开源)。项目名称为“基于SSM的志愿者管理系统”,旨在提升志愿者管理的效率和规范性。通过对系统架构、模块设计及关键代码的深入解析,本文为开发者提供了全面的技术参考和实践指导。 ... [详细]
  • 在HTML5应用中,Accordion(手风琴,又称抽屉)效果因其独特的展开和折叠样式而广泛使用。本文探讨了三种不同的Accordion交互效果,通过层次结构优化信息展示和页面布局,提升用户体验。这些效果不仅增强了视觉效果,还提高了内容的可访问性和互动性。 ... [详细]
  • Sanic 是一个类似于 Flask 的 Python 3.5 Web 服务器,以其出色的写入速度而著称。与 Flask 不同,Sanic 支持异步请求处理,这使得它在处理高并发请求时表现更加出色。通过利用 Python 的异步特性,Sanic 能够显著提高应用程序的性能和响应能力,适用于构建高性能的异步 Web 应用。 ... [详细]
  • 在基于.NET框架的分层架构实践中,为了实现各层之间的松散耦合,本文详细探讨了依赖注入(DI)和控制反转(IoC)容器的设计与实现。通过合理的依赖管理和对象创建,确保了各层之间的单向调用关系,从而提高了系统的可维护性和扩展性。此外,文章还介绍了几种常见的IoC容器实现方式及其应用场景,为开发者提供了实用的参考。 ... [详细]
  • 本文深入探讨了ASP.NET中ViewState、Cookie和Session三种状态管理技术的区别与应用场景。ViewState主要用于保存页面控件的状态信息,确保在多次往返服务器过程中数据的一致性;Cookie则存储在客户端,适用于保存少量用户偏好设置等非敏感信息;而Session则在服务器端存储数据,适合处理需要跨页面保持的数据。文章详细分析了这三种技术的工作原理及其优缺点,并提供了实际应用中的最佳实践建议。 ... [详细]
author-avatar
潮流时候男装
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有