Add a customized header to WebBrowser control for all requests in C++/CLI?

拈花ヽ惹草 提交于 2019-12-08 22:34:28

This is how I solved the problem in C#:

  • Added a reference to SHDocVw. You can find SHDocVw as "Microsoft Internet Controls" in COM tab of Reference manager window.
  • Browse about:blank *Subscribed to BeforeNavigate2 event of DWebBrowserEvents2_Event. I got the instance of the interface from webBrowser1.ActiveXInstance.
  • In BeforeNavigate2 event handler, I check if the header in which I'm interested, doesn't exists in the headers, and if the URL is a http or https URL (and for example is not a javascript: url) and if the request is not for an iframe, then I stop the request and send the request with new headers.

Here is the code which worked for me as expected:

string additionalHeader = "User-Agent:Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; " +
    "Microsoft; Lumia 950 XL Dual SIM) AppleWebKit/537.36 (KHTML, like Gecko) " +
    "Chrome/52.0.2743.116 Mobile Safari/537.36 Edge/15.15063\r\n";
private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.ScriptErrorsSuppressed = true;
    webBrowser1.Navigate("about:blank", null, null, additionalHeader);
    var wbevents = (DWebBrowserEvents2_Event)webBrowser1.ActiveXInstance;
    wbevents.BeforeNavigate2 += Wbevents_BeforeNavigate2;
    webBrowser1.Navigate("http://google.com", null, null, additionalHeader);
}

private void Wbevents_BeforeNavigate2(object pDisp, ref object URL, ref object Flags,
    ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
    if (!$"{Headers}".Contains(additionalHeader) &&
        TargetFrameName == null &&
        $"{URL}".ToLower().StartsWith("http"))
    {
        Cancel = true;
        ((IWebBrowser2)pDisp).Stop();
        object headers = additionalHeader + $"{Headers}";
        object url = $"{URL}";
        object flags = null;
        object targetFrameName = $"{TargetFrameName}";
        ((IWebBrowser2)pDisp).Navigate2(ref url, ref flags,
            ref targetFrameName, ref PostData, ref headers);
    }
}

Note 1 - Navigating will not raise on Refresh

There is a feature/flaw in browser with refresh. When you refresh the browser, Navigating event will not raise, which means the header will not be added in case of refresh. A workaround for that is disabling shortcut and context menu:

this.webBrowser1.WebBrowserShortcutsEnabled = false;
this.webBrowser1.IsWebBrowserContextMenuEnabled = false;

Note 2 - Set user agent

Since this question is about adding header for user agent, there is a better solution for that. You can use the solution which is shared at bottom of this post: Winform Webbrowser being recognized as a mobile device.

It works well even when you refresh the browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!