Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 225285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:23:54+00:00 2026-05-11T19:23:54+00:00

I am trying to change the UserAgent of the WebBrowser control in a Winforms

  • 0

I am trying to change the UserAgent of the WebBrowser control in a Winforms application.

I have successfully achieved this by using the following code:

[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(
    int dwOption, string pBuffer, int dwBufferLength, int dwReserved);

const int URLMON_OPTION_USERAGENT = 0x10000001;

public void ChangeUserAgent()
{
    List<string> userAgent = new List<string>();
    string ua = "Googlebot/2.1 (+http://www.google.com/bot.html)";

    UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, ua, ua.Length, 0);
}

The only problem is that this only works once. When I try to run the ChangeUserAgent() method for the second time it doesn’t work. It stays set to the first changed value. This is quite annoying and I’ve tried everything but it just won’t change more than once.

Does anyone know of a different, more flexible approach?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:23:54+00:00Added an answer on May 11, 2026 at 7:23 pm

    I’m not sure whether I should just copy/paste from a website, but I’d rather leave the answer here, instead of a link. If anyone can clarify in comments, I’ll be much obliged.

    Basically, you have to extend the WebBrowser class.

    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;
                }
            }
        }
    }
    

    Note: To use the method above you’ll need to add a COM reference to “Microsoft Internet Controls”.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to change font globally for whole application, the problem is, i am
Trying to change the selected text background color for an NSTextField (we have a
What I am trying to achieve is the following: I want to have numerous
I'm trying to implement the blog this function from Flickr using the BloggerAPI to
I'm trying to change this request to a HTTP PUT request, any idea how
I'm trying to download this file ( http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar ) with the following method and
I'm trying to change the .show() in my code to .reveal() , and I
I was trying to login to a site remotely. I have successfully done that
Trying to change this: The basketball-player is great! (Kobe Bryant) into this: the basketball
i am trying to change user agent in php.ini file as follows. user_agent=Mozilla/5.0 (iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.