热门标签 | 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请求后的的数据

 



推荐阅读
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 本文介绍了RxJava在Android开发中的广泛应用以及其在事件总线(Event Bus)实现中的使用方法。RxJava是一种基于观察者模式的异步java库,可以提高开发效率、降低维护成本。通过RxJava,开发者可以实现事件的异步处理和链式操作。对于已经具备RxJava基础的开发者来说,本文将详细介绍如何利用RxJava实现事件总线,并提供了使用建议。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
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社区 版权所有