Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not sure whether I should just copy/paste from a <a href="http://wallerdev.com/2008/12/25/changing-the-user-agent-in-ie-net-webbrowser-control-via-csharp" rel="noreferrer">website</a>, but I'd rather leave the answer here, instead of a link. If anyone can clarify in comments, I'll be much obliged.</p> <p>Basically, you have to extend the WebBrowser class.</p> <pre><code>public class ExtendedWebBrowser : WebBrowser { bool renavigating = false; public string UserAgent { get; set; } public ExtendedWebBrowser() { DocumentCompleted += SetupBrowser; //this will cause SetupBrowser to run (we need a document object) Navigate("about:blank"); } void SetupBrowser(object sender, WebBrowserDocumentCompletedEventArgs e) { DocumentCompleted -= SetupBrowser; SHDocVw.WebBrowser xBrowser = (SHDocVw.WebBrowser)ActiveXInstance; xBrowser.BeforeNavigate2 += BeforeNavigate; DocumentCompleted += PageLoaded; } void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e) { } void BeforeNavigate(object pDisp, ref object url, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel) { if (!string.IsNullOrEmpty(UserAgent)) { if (!renavigating) { headers += string.Format("User-Agent: {0}\r\n", UserAgent); renavigating = true; cancel = true; Navigate((string)url, (string)targetFrameName, (byte[])postData, (string)headers); } else { renavigating = false; } } } } </code></pre> <p>Note: To use the method above you’ll need to add a COM reference to “Microsoft Internet Controls”.</p> <p>He mentions your approach too, and states that the WebBrowser control seems to cache this user agent string, so it will not change the user agent without restarting the process.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload