5

I'm making an application that contains a WebBrowser element and I would like to have the application show the useragent for that user's default browser.

I know how to get both the default browser via Registry keys and how to get the user agent for a browser but cannot figure out how to combine the two. Is this possible?

svick
  • 236,525
  • 50
  • 385
  • 514
Mr Wednesday
  • 552
  • 4
  • 16
  • 1
    Considering that a webbrowser can put anything into its UserAgent (e.g. the current time), then I think you actually need to start the browser to do that reliably. The question is, why do you even want to know this? – svick May 07 '12 at 12:45
  • I'm trying to make the application imitate the user's default browser. – Mr Wednesday May 07 '12 at 12:52
  • 2
    And why do you want to do that? Usually, doing that is not a good idea. – svick May 07 '12 at 12:54

3 Answers3

1

What I would do (of course, this is a bit of an overkill here) is to include a web server and request an URL from this web server, thus getting the user agent.

I.e. roughly this would include:

  1. Implement a web server inside the application, e.g. this one
  2. Let the WebBrowser control call a local URL of the webserver (e.g. http://127.0.0.1:48384/test)
  3. In the web server's request handler, store the user agent into a variable
  4. Show this variable to the end user (e.g. in a Label control on your WinForm app or simply by sending a response back from the webserver.

I've successfully used the web server inside my applications several times. One example would by my HTML edit control over at the Code Project.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Sorry, I'm not sure I see how this gets the user agent for the default browser? Wouldn't this return the useragent for the webbrowser, a value I have set. – Mr Wednesday May 07 '12 at 12:27
  • Yes, this would return of the `WebBrowser` control. After all, since you mentioned the `WebBrowser` control, I read that you want to get this user agent. – Uwe Keim May 07 '12 at 13:01
  • So how is your mention of `WebBrowser` related to the "default browser" you also mention? – Uwe Keim May 07 '12 at 13:01
  • The useragent of the default browser will be applied to the aforementioned WebBrowser. – Mr Wednesday May 07 '12 at 13:41
0

Try this(this is a simple function to check if the browser is of a handheld device)

string strUserAgent = Request.UserAgent.ToString().ToLower();
bool status = false;
if (strUserAgent != null)
{
    if (Request.Browser.IsMobileDevice == true || 
        strUserAgent.Contains("iphone") ||
        strUserAgent.Contains("blackberry") || 
        strUserAgent.Contains("mobile") ||
        strUserAgent.Contains("windows ce") || 
        strUserAgent.Contains("opera mini") ||
        strUserAgent.Contains("palm"))
    {
        status = true;
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Paras
  • 2,997
  • 6
  • 35
  • 46
0

Here is a pretty simple class to get and set UserAgent.

using System.Runtime.InteropServices;

public static class UserAgent
{

    [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
    private static extern int UrlMkGetSessionOption(
        int dwOption, StringBuilder pBuffer, int dwBufferLength, out int pdwBufferLength, int dwReserved);
    [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;
    const int URLMON_OPTION_USERAGENT_REFRESH = 0x10000002;
    const int URLMON_OPTION_URL_ENCODING = 0x10000004;

    public static string Value
    {
        get
        {
            StringBuilder builder = new StringBuilder(512);
            int returnLength;
            UrlMkGetSessionOption(URLMON_OPTION_USERAGENT, builder, builder.Capacity, out returnLength, 0);
            string value = builder.ToString();
            return value;
        }
        set
        {
            UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, value, value.Length, 0);
        }
    }
}

I had been looking for a decent way to get UserAgent from windows, as opposed to web, when I found Changing the user agent of the WebBrowser control Inspired by the DLLImport of urlmon.dll, I looked in pinvoke.net, and found: http://pinvoke.net/default.aspx/urlmon.UrlMkGetSessionOption

Called by:

namespace GetUserAgent
{
    class Program
    {
        //  The name of the program that was started by user
        //  See: Assembly.GetEntryAssembly Method ()
        //  https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getentryassembly.aspx
        public static string ExecName
        {
            get
            {
                //  The name of the executable that was started by user
                return System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
            }
        }

        static void Main(string[] args)
        {
            foreach (string arg in args)
            {
                if ((arg == "/?")
                    || (arg == "-?")
                    || (arg == "--help"))
                {
                    Console.Out.WriteLine(
                        "For /F \"tokens=*\" %%U In ('{0}') Do Set UserAgent=%%U",
                        ExecName);
                    return;
                }
            }

            string userAgent = UserAgent.Value;
            Console.Out.WriteLine(userAgent);
        }
    }
}

This generates the UserAgent value. Argument /? shows how to use it in a command script.