我在我的Winform
项目中使用CefSharp
,我想仅为特定浏览器更改用户代理。
我通过使用自定义IRequestHandler
并在那里设置用户代理来实现:
class IGRequestHandler : IRequestHandler
{
public bool CanGetCookies(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request)
{
return true;
}
public bool CanSetCookie(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, Cookie cookie)
{
return true;
}
public bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
callback.Dispose();
return false;
}
public IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return null;
}
public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
{
return false;
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var headers = request.Headers;
headers["User-Agent"] = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A403 Safari/602.1";
request.Headers = headers;
return CefReturnValue.Continue;
}
public bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
callback.Dispose();
return false;
}
public bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}
public void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath)
{
}
public bool OnProtocolExecution(IWebBrowser chromiumWebBrowser, IBrowser browser, string url)
{
return false;
}
public bool OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
callback.Dispose();
return false;
}
public void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status)
{
}
public void OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
}
public void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
}
public void OnResourceRedirect(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl)
{
}
public bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
}
public bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, System.Security.Cryptography.X509Certificates.X509Certificate2Collection certificates, ISelectClientCertificateCallback callback)
{
callback.Dispose();
return false;
}
}
当我用这个加载页面时,我仍然得到网站的普通版本,而不是移动的版本。
当我使用以下命令更改用户代理时:
var settings = new CefSettings();
settings.CachePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\CEF";
settings.IgnoreCertificateErrors = true;
settings.WindowlessRenderingEnabled = true;
settings.SetOffScreenRenderingBestPerformanceArgs();
settings.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A403 Safari/602.1";
Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
我得到的移动的版本,但我想做的真实的,而不是每一个浏览器.
这就是我如何实现浏览器:
instaBrowser = new ChromiumWebBrowser("");
instaBrowser.RequestHandler = new IGRequestHandler();
instaBrowser.Dock = DockStyle.Fill;
instaBrowser.FrameLoadEnd += webViewFrameLoadEnd;
instaBrowser.LoadError += getFromBrowser_LoadError;
instaBrowser.BrowserSettings.Plugins = CefState.Disabled;
instaBrowser.BrowserSettings.WebGl = CefState.Disabled;
instaBrowser.BrowserSettings.RemoteFonts = CefState.Disabled;
instaBrowser.BrowserSettings.WindowlessFrameRate = 1;
instaBrowser.JsDialogHandler = new JsDialogHandler();
instaPanel.Controls.Add(instaBrowser);
1条答案
按热度按时间k4aesqcs1#
这是很难找到的,但它似乎是:
即使浏览器对象已经创建,也能成功更改用户代理。
例如,我有这个方法,它正在处理“OnIsBrowserInitializedChanged”事件:
当我这么做的时候:
网站显示的字符串与我输入的完全相同。