Cefsharp: Accept header is overwritten and cannot be set

Created on 26 Mar 2019  路  4Comments  路  Source: cefsharp/CefSharp

After calling the LoadRequest method, the Accept header is overwritten and cannot be set.

I overrode the LoadUrlWithPostData method through IFrame.LoadRequest(IRequest). ACCEPT header was added to IRequeset.Headers, but were eventually overwritten. And I don't want to set it up in the OnBeforeResourceLoad method, although it's an effective way.

private static void LoadUrl(ChromiumWebBrowser browser, string url, byte[] postDataBytes, string requestMethod = "POST", WebHeaderCollection headers = null)
        {
            using (var frame = browser.GetMainFrame())
            {
                ThrowExceptionIfFrameNull(frame);


                var initializePostData = requestMethod.ToLower() == "post";

                var request = frame.CreateRequest(initializePostData: initializePostData);
                if (initializePostData)
                {
                    request.InitializePostData();
                    request.PostData.AddData(postDataBytes);
                }

                request.Url = url;
                request.Method = requestMethod;

                if (headers != null && headers.HasKeys())
                {
                    var originHeader = request.Headers ?? new NameValueCollection();

                    foreach (string keyName in headers.AllKeys)
                    {
                        originHeader.Set(keyName, headers[keyName]);
                    }

                    var refererValue = headers[HttpRequestHeader.Referer];

                    // Set Referer
                    if (!string.IsNullOrEmpty(refererValue))
                    {
                        request.SetReferrer(refererValue, ReferrerPolicy.Default);
                    }


                    request.Headers = originHeader;
                }


                frame.LoadRequest(request);
            }
        }

        public static void LoadUrlWithPostDataLocal(this ChromiumWebBrowser browser, string url, byte[] postDataBytes, WebHeaderCollection headers = null)
        {
            LoadUrl(browser, url, postDataBytes, "POST", headers);
        }

Here is my example:

var postdata = "mact=1&search_flag=1&page=1&search_text=";
var buffer = Encoding.UTF8.GetBytes(postdata);
var cookies = "";
var headers = new WebHeaderCollection
{
        {"Accept","application/json, text/javascript, */*; q=0.01" },
        {HttpRequestHeader.Cookie,cookies }
};
//  Accept header will be rewritten  
browser.LoadUrlWithPostDataLocal(url, buffer, header);

Does anyone help me?

upstream

All 4 comments

:wave: @DonetCracker, we use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.
If you have a question, ask it on Gitter, StackOverflow or use ceforum (for questions specific to CEF).
If you are new to CefSharp then we suggest you read https://github.com/cefsharp/CefSharp/wiki/General-Usage

For reference LoadUrlWithPostData has been marked as Obsolete and will be removed (#2705)

LoadRequest bypasses the normal navigation pipeline

https://magpcss.org/ceforum/viewtopic.php?f=6&t=16592&p=41067#p41074

There are many problems with LoadRequest in 71.0.x, these are CEF issues.

And I don't want to set it up in the OnBeforeResourceLoad method, although it's an effective way.

That is the only reliably way. LoadRequestis just unreliable now, it worked in older versions.

More specifically it looks like your issue is https://magpcss.org/ceforum/viewtopic.php?f=6&t=16561&p=40960.

Closing as upstream.

More specifically it looks like your issue is https://magpcss.org/ceforum/viewtopic.php?f=6&t=16561&p=40960.

Closing as upstream.

Thank you very much, you save my life!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SimonTheCat picture SimonTheCat  路  3Comments

marci4 picture marci4  路  4Comments

mihailik picture mihailik  路  4Comments

amaitland picture amaitland  路  3Comments

jdc20181 picture jdc20181  路  4Comments