Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept header is overwritten and cannot be set #2704

Closed
DonetCracker opened this issue Mar 26, 2019 · 4 comments
Closed

Accept header is overwritten and cannot be set #2704

DonetCracker opened this issue Mar 26, 2019 · 4 comments
Labels
upstream These issues require fixing in the Chromium Embedded Framework(CEF) or Chromium.

Comments

@DonetCracker
Copy link

DonetCracker commented Mar 26, 2019

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?

@support
Copy link

support bot commented Mar 26, 2019

👋 @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

@support support bot closed this as completed Mar 26, 2019
@amaitland
Copy link
Member

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. LoadRequest is just unreliable now, it worked in older versions.

@amaitland amaitland added upstream These issues require fixing in the Chromium Embedded Framework(CEF) or Chromium. and removed question support labels Mar 26, 2019
@support support bot reopened this Mar 26, 2019
@amaitland
Copy link
Member

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

Closing as upstream.

@DonetCracker
Copy link
Author

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!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream These issues require fixing in the Chromium Embedded Framework(CEF) or Chromium.
Projects
None yet
Development

No branches or pull requests

2 participants