CefSharp for winform 设置 UserAgent

CefSharp_for_winform_设置_UserAgent



简单:调用一个 CusRequestHandler.cs  就可以了




        public Form1()
        {
            InitializeComponent();
            chromiumWebBrowser1.RequestHandler = new CusRequestHandler();
        }


        private void Form1_Load(object sender, EventArgs e)
        {




            chromiumWebBrowser1.LoadUrl("https://m.ixigua.com/");

        }


}





CusRequestHandler 文件代码:


using CefSharp;
using CefSharp.Handler;
using CefSharp.ResponseFilter;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public class CusRequestHandler : RequestHandler
{
    protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
    {
        return new CusResourceRequestHandler();
    }
}

public class CusResourceRequestHandler : ResourceRequestHandler
{
    private MemoryStream memoryStream;
    protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
    {
        string MobileAgent = "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36";

        request.SetHeaderByName("user-agent", MobileAgent, true);

        return CefReturnValue.Continue;
    }
}








winform代码
桂ICP备16004754号