• VC#

Changing UserAgent in C#'s WebBrowser

To set iPhone's UserAgent to C#'s WebBrowser, you can do the following. With this method, only one kind of UA can be used. I'm an amateur in both C# and .net, so I don't really understand the principle, but with this code, the UA was actually rewritten and the screen for iPhone was displayed.

[cc lang="c#"]

namespace WindowsFormsApplication
public partial class SomeApp: Form

[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]]
private static extern int UrlMkSetSessionOption(int dwOption, string str, int nLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;
string ua = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en)";

public SomeApp()
UrlMkSetSessionOption(UrlMON_OPTION_USERAGENT, ua, ua.Length, 0);

Rewrite UserAgent in IE component" windows application