CefSharp实时更改UserAgent

时间:2019-01-29 09:29:53

标签: c# winforms cefsharp

我在我的CefSharp项目中使用Winform,并且我只想为特定的浏览器更改User-Agent。

我通过使用自定义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 个答案:

答案 0 :(得分:1)

这真的很难找,但看起来像是:

NSURLConnection

即使已创建浏览器对象,也成功更改用户代理。

例如,我有此方法,该方法处理“ OnIsBrowserInitializedChanged”事件:

browser.GetDevToolsClient().Emulation.SetUserAgentOverrideAsync("");

当我这样做时:

    private void OnIsBrowserInitializedChanged(object sender, EventArgs e)
    {
        using (DevToolsClient DTC = browser.GetDevToolsClient()) {
            DTC.Emulation.SetUserAgentOverrideAsync("Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; SCH-I535 Build/KOT49H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
        }
    }

该网站向我显示了与输入的字符串完全相同的字符串。