c# cefsharp截取request 代码
时间: 2023-12-08 17:29:37 浏览: 171
使用 CefSharp 截取请求的代码如下:
```csharp
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CefSharpExample
{
public partial class MainForm : Form
{
private ChromiumWebBrowser browser;
public MainForm()
{
InitializeComponent();
InitializeChromium();
}
private void InitializeChromium()
{
CefSettings settings = new CefSettings();
settings.CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache");
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("https://www.google.com");
browser.FrameLoadEnd += Browser_FrameLoadEnd;
browser.RequestHandler = new RequestHandler();
browser.Dock = DockStyle.Fill;
this.Controls.Add(browser);
}
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain)
{
browser.ExecuteScriptAsync("alert('Page loaded.');");
}
}
public class RequestHandler : IRequestHandler
{
public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
{
return false;
}
public bool OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
Console.WriteLine(request.Url);
return false;
}
public bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
return false;
}
public bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
return false;
}
public void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath)
{
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResourceRequestCallback callback)
{
return CefReturnValue.Continue;
}
public bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
}
public void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)
{
}
public bool OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
return false;
}
public void OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
}
public bool OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, string url)
{
return false;
}
public void OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public bool OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
return false;
}
public void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, HttpStatusCode status)
{
}
public void OnProtocolExecutionComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, bool wasSuccessful)
{
}
public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
return false;
}
public void OnDocumentAvailableInMainFrame(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public void OnFrameAttached(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
public void OnFrameDetached(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
public void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath, string pluginVersion)
{
}
public void OnRenderFrameReady(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
public bool OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
}
}
}
}
```
在这个例子中,我们创建了一个 `ChromiumWebBrowser` 控件并访问了 Google 主页。我们在 `RequestHandler` 类中实现了 `OnBeforeResourceLoad` 方法来截取请求,并在控制台输出请求的 URL。
阅读全文