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

Error processing SSI file

Answers

  1. Chaim

    • 2017/10/29

    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.

  2. Jase

    • 2019/1/22

    I'm using C# and .NET 3.5. As you know, we can add custom header to Navigate() on the web browser control like this: var 

  3. Brooks

    • 2021/1/5

    As you know, we can add custom header to Navigate () on the web browser control like this: var myUrl = "http://example.com/mypage.htm"; System.Uri uri = new Uri (myUrl); byte [] authData = System.Text.UnicodeEncoding.UTF8.GetBytes ("user:password"); string authHeader = "Authorization: Basic " + Convert.ToBase64String (authData) + "\r " + "User-Agent: MyUserAgent\r "; webTDW8961nd.Navigate (uri, "", null, authHeader);

  4. Alan

    • 2020/8/29

    I saw that it is an overloaded function to add a custom http header but it is available only for POST method??

  5. Davion

    • 2018/12/18

    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:

  6. Odin

    • 2016/3/12

    The WebBrowser control lets you host Web pages and other browser-enabled documents in your Windows Forms applications. You can use the WebBrowser control, for 

  7. Maurice

    • 2019/8/2

    Answer #2: To add custom header for each request you can implement extension method: public static class WebBrowserExtensions { public static void NavigateWithAuthorization(this WebBrowser browser, Uri uri) { byte[] authData = System.Text.Encoding.UTF8.GetBytes ("user:password"); string authHeader = "Authorization: Basic " + Convert.ToBase64String (authData) + " " + "User-Agent: MyUserAgent "; browser.Navigate (uri, "", null, authHeader); } }

  8. Dane

    • 2018/6/27

    Sets a default HTTP request header for every subsequent object created by this WebBrowser, using the contents of the specified string.

  9. Joel

    • 2017/10/19

    script to the htm page. In the javascript call the url with custom http header and reditrect to the original URL. From webbrowser control navigate to the dummy htm file. xhr = new XMLHttpRequest(); xhr.open('post', p_url, true); xhr.setRequestHeader('<HEADER NAME>','<VALUE>'); xhr.onreadystatechange = showPageContent; xhr.send(null);

  10. Izaiah

    • 2017/3/28

    I saw that it is an overloaded function to add a custom http header but it is available only for POST method It is very easy to do this with VB6 but .

  11. Azrael

    • 2020/7/11

    2 Answers2. Active Oldest Votes. 0. try this links for getting web browser request and response: Http-Monitor-for-Webbrowser-Control. capturing-all-requests-and-responses-from-the-web-browser-control. Share. Improve this answer. edited May 23 '17 at 11:53.

  12. Gallo

    • 2018/10/16

    If you're using the Web Browser Control or the Internet. I will demonstrate how to use the WebBrowser control in a Windows Forms application using C# 

  13. Marquis

    • 2017/1/16

    In this application, I am going to add a ToolStrip and WebBrowser control to the form. In my ToolStrip control, I add a Label, a TextBox, and a few Button controls with a few separators. By the time we are done with the application, the final user interface will look like Figure 1.

  14. Juelz

    • 2017/12/23

    C# queries related to “c# webbrowser control append” · add web browser at runn time c# winforms · enable webbrowser control in c# toolbox · web 

  15. Kylen

    • 2017/4/18

    In the Connections pane, go to the site, application, or directory for which you want to set a custom HTTP header. In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

  16. Casen

    • 2018/7/8

    NET and, 905 Web server controls for, 23 Web server, client deployment from, 1004–1005 creating client for, 991–992 declaring custom SOAP header, 

  17. Dante

    • 2019/5/26

    Advanced customization and total control over the Webbrowser An HTTP and HTTPS request and response header viewer which can be used for 

  18. Bodie

    • 2020/6/29

    an example of C# HttpClient Get request · Step 1.Create an HttpClient instance correctly in Controller · step 2.Adding Custom Http Headers to HttpClient before 

Comments are closed.

More Posts