我现在正在使用浏览器构建应用程序,当用户输入URL时,我的程序将首先检测到URL是否重定向到其他链接。如果我的程序检测到重定向URL包含一些关键字,它将停止加载页面并返回主程序。
我已经知道我已经实现了IRequestHandler来获取重定向URL,但是我不知道如何停止连续加载页面并保存状态然后返回主程序。请帮助谢谢
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.Threading;
namespace Chrome
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitBrowser();
}
private Thread m_ThreadDetect;
public ChromiumWebBrowser browser;
public void InitBrowser()
{
var setting = new CefSettings();
setting.CachePath = "/BrowserCache";
setting.PersistSessionCookies = true;
Cef.Initialize(setting);
browser = new ChromiumWebBrowser("www.testing.com")
{
RequestHandler = new MyRequestHandler()
};
this.panel1.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
private void button_go_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox_URL.Text) || textBox_URL.Text.Equals("about:blank"))
{
MessageBox.Show("Enter a valid URL.");
textBox_URL.Focus();
return;
}
browser.Load(textBox_URL.Text);
///////////////////////////////////////
//Check whether the browser already catch the keyword in here
///////////////////////////
}
private void button_search_Click(object sender, EventArgs e)
{
browser.Find(0, textBox_search.Text, true, false, false);
}
private async Task getSource()
{
string htmlText = await browser.GetBrowser().MainFrame.GetSourceAsync();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using CefSharp;
using System.Collections;
using System.Windows.Forms;
using System.Collections.Specialized;
namespace Chrome
{
class MyRequestHandler : IRequestHandler
{
public bool CanGetCookies(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
//throw new NotImplementedException();
return true;
}
public bool CanSetCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, Cookie cookie)
{
//throw new NotImplementedException();
return true;
}
public bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
throw new NotImplementedException();
}
public IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
Console.WriteLine("GetResourceResponseFilter");
//throw new NotImplementedException();
return null;
}
public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
//throw new NotImplementedException();
Console.WriteLine("OnBeforeBrowse");
Console.WriteLine("OnBeforeBrowse, isRedirect"+ isRedirect);
return false;
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
//throw new NotImplementedException();
Console.WriteLine("OnBeforeResourceLoad");
return CefReturnValue.Continue;
}
public bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
throw new NotImplementedException();
}
public bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
throw new NotImplementedException();
}
public void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath)
{
throw new NotImplementedException();
}
public bool OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, string url)
{
throw new NotImplementedException();
}
public bool OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
throw new NotImplementedException();
}
public void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)
{
throw new NotImplementedException();
}
public void OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
//throw new NotImplementedException();
}
public void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
Console.WriteLine("OnResourceLoadComplete");
//throw new NotImplementedException();
}
public void OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
//Console.WriteLine(response.Headers);
//detect keyword
if(location.Equals("Redirect"))
{
chromiumWebBrowser.Stop();
Console.WriteLine("Detect Stop");
}
Console.WriteLine("Done");
Console.WriteLine("OnResourceRedirect");
Console.WriteLine(newUrl);
//throw new NotImplementedException();
}
public bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//throw new NotImplementedException();
Console.WriteLine("here");
return false;
}
public bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
{
throw new NotImplementedException();
}
}
}