0

I need to change the User-Agent of my webbrowser control. But no using the overload of the Navigating method ( because the script get the user agent not from the header, but from the window.navigator.appName property. Is there a way to change this property in the webbrowser??

Thanks!

gog
  • 11,788
  • 23
  • 67
  • 129

1 Answers1

2

update: window.navigator should be a property you can overwrite via IDispatchEx (IExpando on the CLR runtime wrapper) in Javascript. something like

var document=webBrowser1.Document.DomDocument as MSHTML.IHTMLDocument2;
var expando =(IExpando)document.parentWindow;
expando.RemoveProperty(expando.GetMember("navigator",BindingFlags.Instance | BindingFlags.Public));
expando.AddProperty("navigator").SetValue(expando,myNavigator);

here myNavigator is a variable of a COM visible type that has an appName property.

not working: pinvoke UrlMkSetSessionOption and pass URLMON_OPTION_USERAGENT as the option flag.

Community
  • 1
  • 1
Sheng Jiang 蒋晟
  • 15,125
  • 2
  • 28
  • 46
  • unfortunatelly it just send a header with the user agent. – gog Oct 16 '15 at 15:51
  • Very nice answer, i can get the property navigator, but i cannot set the value of the navigator.appName yet. but im still trying – gog Oct 16 '15 at 16:23
  • @ggui you have a race condition if the value is read during page load. – Sheng Jiang 蒋晟 Oct 16 '15 at 16:53
  • the expando.RemoveMember takes a MemberInfo as parameter, of system.Reflection. – gog Oct 16 '15 at 16:55
  • Could you take a look in this question ? http://stackoverflow.com/questions/33172010/how-to-get-a-image-using-webbrowser?noredirect=1#comment54152127_33172010 – gog Oct 16 '15 at 16:57