P
P
Proger59132021-03-26 21:16:21
C++ / C#
Proger5913, 2021-03-26 21:16:21

How to change Headers in CefSharp?

Purpose: to create a separate program for the site, i.e. so that the site can be opened with this program. To do this, you need to pass the referer header. The thing is that CefSharp does not give me the opportunity to do this, or I'm doing something wrong.
On startup, an exception is thrown:

"IRequest is read-only and cannot be modified. Check IRequest.IsReadOnly to guard against this exception."


The code:
using CefSharp;
using CefSharp.Handler;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewDeaf
{
    public partial class NewDeafForm : Form
    {
        public NewDeafForm()
        {
            InitializeComponent();
        }

        public ChromiumWebBrowser wb;

        public void InitializeChromium()
        {
            CefSettings settings = new CefSettings();
            Cef.Initialize(settings);
            wb = new ChromiumWebBrowser("https://newdeaf.site")
            {
                RequestHandler = new ChromeBrowserRequestHandler()
            };
            WbPanel.Controls.Add(wb);
            wb.Dock = DockStyle.Fill;
        }

        private void NewDeafForm_Load(object sender, EventArgs e)
        {
            InitializeChromium();
            wb.Load("https://newdeaf.site");
            wb.RequestHandler.OnBeforeBrowse(wb, null, null, Request.Create(), false, false);
        }
    }

    class ChromeBrowserRequestHandler : RequestHandler
    {
        protected override bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
        {
            request.SetReferrer("https://KiIzEDPhB0.com", ReferrerPolicy.ClearReferrerOnTransitionCrossOrigin);
            return false;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2021-03-26
@BasiC2k

Setting Headers:
CefSettings settings = new CefSettings();
settings.CefCommandLineArgs.Add("accept-encoding", "gzip, deflate, br");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question