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?

3
  • 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? Commented May 7, 2012 at 12:45
  • I'm trying to make the application imitate the user's default browser. Commented May 7, 2012 at 12:52
  • 2
    And why do you want to do that? Usually, doing that is not a good idea. Commented May 7, 2012 at 12:54

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
So how is your mention of WebBrowser related to the "default browser" you also mention?
The useragent of the default browser will be applied to the aforementioned WebBrowser.
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; } } 

1 Comment

That's not what is being asked at all.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.