当前位置:   article > 正文

c 窗体怎么去服务器上获取文件,WinForm窗体程序中使用CefSharp获取加载后的资源、截取request参数、拦截response数据、注入jquery文件和js代码(2)-截取request...

cefsharp51 打印请求文件的源码

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

四、cefsharp截取request参数

所有的request数据都可以在接口类IRequestHandler的方法中获取到,不过要先实现这些方法。

1、添加类 RequestHandler,继承IRequestHandler,实现接口中的各种方法

public classRequestHandler : IRequestHandler

{

///

bool IRequestHandler.OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, boolisRedirect)

{

returnOnBeforeBrowse(chromiumWebBrowser, browser, frame, request, userGesture, isRedirect);

}

///

/// Called before browser navigation. If the navigation is allowed and

///

/// will be called. If the navigation is canceled will be called with an ErrorCode value of

/// .

///

/// the ChromiumWebBrowser control.

/// the browser object.

/// The frame the request is coming from.

/// the request object - cannot be modified in this callback.

/// The value will be true if the browser navigated via explicit user gesture (e.g. clicking a link) or

/// false if it navigated automatically (e.g. via the DomContentLoaded event).

/// has the request been redirected.

///

/// Return true to cancel the navigation or false to allow the navigation to proceed.

///

protected virtual bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, boolisRedirect)

{

return false;

}

///

bool IRequestHandler.OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, stringtargetUrl,

WindowOpenDisposition targetDisposition, booluserGesture)

{

returnOnOpenUrlFromTab(chromiumWebBrowser, browser, frame, targetUrl, targetDisposition, userGesture);

}

///

/// Called on the UI thread before OnBeforeBrowse in certain limited cases where navigating a new or different browser might be

/// desirable. This includes user-initiated navigation that might open in a special way (e.g. links clicked via middle-click or

/// ctrl + left-click) and certain types of cross-origin navigation initiated from the renderer process (e.g. navigating the top-

/// level frame to/from a file URL).

///

/// the ChromiumWebBrowser control.

/// the browser object.

/// The frame object.

/// target url.

/// The value indicates where the user intended to navigate the browser based on standard

/// Chromium behaviors (e.g. current tab, new tab, etc).

/// The value will be true if the browser navigated via explicit user gesture (e.g. clicking a link) or

/// false if it navigated automatically (e.g. via the DomContentLoaded event).

///

/// Return true to cancel the navigation or false to allow the navigation to proceed in the source browser's top-level frame.

///

protected virtual bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, stringtargetUrl,

WindowOpenDisposition targetDisposition, booluserGesture)

{

return false;

}

///

IResourceRequestHandler IRequestHandler.GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref booldisableDefaultHandling)

{

return GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator, refdisableDefaultHandling);

}

///

/// Called on the CEF IO thread before a resource request is initiated.

///

/// the ChromiumWebBrowser control.

/// represent the source browser of the request.

/// represent the source frame of the request.

/// represents the request contents and cannot be modified in this callback.

/// will be true if the resource request is a navigation.

/// will be true if the resource request is a download.

/// is the origin (scheme + domain) of the page that initiated the request.

/// [in,out] to true to disable default handling of the request, in which case it will need

/// to be handled via or it will be canceled.

///

/// To allow the resource load to proceed with default handling return null. To specify a handler for the resource return a

/// object. If this callback returns null the same method will be called on the associated

/// , if any.

///

protected virtual IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref booldisableDefaultHandling)

{

return null;

}

///

bool IRequestHandler.GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, stringhost,

int port, string realm, stringscheme, IAuthCallback callback)

{

returnGetAuthCredentials(chromiumWebBrowser, browser, originUrl, isProxy, host, port, realm, scheme, callback);

}

///

/// Called when the browser needs credentials from the user.

///

/// The ChromiumWebBrowser control.

/// the browser object.

/// is the origin making this authentication request.

/// indicates whether the host is a proxy server.

/// hostname.

/// port number.

/// realm.

/// scheme.

/// Callback interface used for asynchronous continuation of authentication requests.

///

/// Return true to continue the request and call when the authentication

/// information is available. Return false to cancel the request.

///

protected virtual bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, stringhost,

int port, string realm, stringscheme, IAuthCallback callback)

{

callback.Dispose();

return false;

}

///

bool IRequestHandler.OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, longnewSize,

IRequestCallback callback)

{

returnOnQuotaRequest(chromiumWebBrowser, browser, originUrl, newSize, callback);

}

///

/// Called when JavaScript requests a specific storage quota size via the webkitStorageInfo.requestQuota function. For async

/// processing return true and execute at a later time to grant or deny the request or

/// to cancel.

///

/// The ChromiumWebBrowser control.

/// the browser object.

/// the origin of the page making the request.

/// is the requested quota size in bytes.

/// Callback interface used for asynchronous continuation of url requests.

///

/// Return false to cancel the request immediately. Return true to continue the request and call

/// either in this method or at a later time to grant or deny the request.

///

protected virtual bool OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, longnewSize,

IRequestCallback callback)

{

callback.Dispose();

return false;

}

///

bool IRequestHandler.OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, stringrequestUrl,

ISslInfo sslInfo, IRequestCallback callback)

{

returnOnCertificateError(chromiumWebBrowser, browser, errorCode, requestUrl, sslInfo, callback);

}

///

/// Called to handle requests for URLs with an invalid SSL certificate. Return true and call

/// either in this method or at a later time to continue or cancel the request.

/// If CefSettings.IgnoreCertificateErrors is set all invalid certificates will be accepted without calling this method.

///

/// the ChromiumWebBrowser control.

/// the browser object.

/// the error code for this invalid certificate.

/// the url of the request for the invalid certificate.

/// ssl certificate information.

/// Callback interface used for asynchronous continuation of url requests. If empty the error cannot be

/// recovered from and the request will be canceled automatically.

///

/// Return false to cancel the request immediately. Return true and use to execute in an async

/// fashion.

///

protected virtual bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, stringrequestUrl,

ISslInfo sslInfo, IRequestCallback callback)

{

callback.Dispose();

return false;

}

///

bool IRequestHandler.OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, intport,

X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)

{

returnOnSelectClientCertificate(chromiumWebBrowser, browser, isProxy, host, port, certificates, callback);

}

///

/// Called when the browser needs user to select Client Certificate for authentication requests (eg. PKI authentication).

///

/// The ChromiumWebBrowser control.

/// the browser object.

/// indicates whether the host is a proxy server.

/// hostname.

/// port number.

/// List of Client certificates for selection.

/// Callback interface used for asynchronous continuation of client certificate selection for

/// authentication requests.

///

/// Return true to continue the request and call ISelectClientCertificateCallback.Select() with the selected certificate for

/// authentication. Return false to use the default behavior where the browser selects the first certificate from the list.

///

///

protected virtual bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, intport,

X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)

{

callback.Dispose();

return false;

}

///

void IRequestHandler.OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, stringpluginPath)

{

OnPluginCrashed(chromiumWebBrowser, browser, pluginPath);

}

///

/// Called when a plugin has crashed.

///

/// the ChromiumWebBrowser control.

/// the browser object.

/// path of the plugin that crashed.

protected virtual void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, stringpluginPath)

{

}

///

voidIRequestHandler.OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)

{

OnRenderViewReady(chromiumWebBrowser, browser);

}

///

/// Called on the CEF UI thread when the render view associated with browser is ready to receive/handle IPC messages in the

/// render process.

///

/// The ChromiumWebBrowser control.

/// the browser object.

protected virtual voidOnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)

{

}

///

voidIRequestHandler.OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)

{

OnRenderProcessTerminated(chromiumWebBrowser, browser, status);

}

///

/// Called when the render process terminates unexpectedly.

///

/// The ChromiumWebBrowser control.

/// the browser object.

/// indicates how the process terminated.

protected virtual voidOnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)

{

}

///

voidIRequestHandler.OnDocumentAvailableInMainFrame(IWebBrowser chromiumWebBrowser, IBrowser browser)

{

OnDocumentAvailableInMainFrame(chromiumWebBrowser, browser);

}

///

/// Called on the CEF UI thread when the window.document object of the main frame has been created.

///

/// the ChromiumWebBrowser control

/// the browser object

protected virtual voidOnDocumentAvailableInMainFrame(IWebBrowser chromiumWebBrowser, IBrowser browser)

{

}

}

2、新建WinFormsRequestHandler类,再继承RequestHandler类,在WinFormsRequestHandler类中来写具体的获取request参数方法

比如,要获取百度登录的post参数

先F12查看此方法名称为login

WinFormsRequestHandler类中重写覆盖父类中的方法GetResourceRequestHandler,并且只拦截请求url中有login名称的请求

public classWinFormsRequestHandler : RequestHandler

{

protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref booldisableDefaultHandling)

{

//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 inelements)

{

if (element.Type ==PostDataElementType.Bytes)

{

var body =element.GetBody(charSet);

}

}

}

}

}

return null;

}

}

3、要在初始化cefsharp的地方,指向request请求的具体实现

public static string url = "https://www.baidu.com";//设置进入网页

public ChromiumWebBrowser browser = new ChromiumWebBrowser(url);//初始化cefsharp浏览器

publicForm1()

{

InitializeComponent();

browser.Dock = DockStyle.Fill;//设置填充满窗体

this.Controls.Add(browser);//把cefsharp加入到窗体中

browser.RequestHandler = new WinFormsRequestHandler();//request请求的具体实现

}

4、调试运行,在步骤2的body中就可以获取到request的参数。再用&分割就可以得到各个参数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/257783
推荐阅读
相关标签
  

闽ICP备14008679号