usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingSystem.Reflection;namespaceWindowsFormsApplication2 { Public classUseragenthelper {Private Static stringDefaultuseragent =NULL; [DllImport ("Urlmon.dll", CharSet =CharSet.Ansi)]Private Static extern intUrlmksetsessionoption (intDwoption,stringPbuffer,intDwbufferlength,intdwreserved); Const intUrlmon_option_useragent =0x10000001; /// <summary> ///add a portion after the default useragent/// </summary> Public Static voidAppenduseragent (stringappenduseragent) { if(string. IsNullOrEmpty (defaultuseragent)) Defaultuseragent=getdefaultuseragent (); stringUA = Defaultuseragent";"appenduseragent; Changeuseragent (UA); } /// <summary> ///Modify UserAgent/// </summary> Public Static voidChangeuseragent (stringuseragent) {urlmksetsessionoption (urlmon_option_useragent, useragent, Useragent.length,0); } /// <summary> ///a very bt way to get IE default useragent/// </summary> Private Static stringgetdefaultuseragent () {WebBrowser WB=NewWebBrowser (); Wb. Navigate ("About:blank"); while(WB. IsBusy) application.doevents (); Objectwindow =WB. Document.Window.DomWindow; Type WT=window. GetType (); ObjectNavigator = wt. InvokeMember ("Navigator", BindingFlags.GetProperty,NULL, window,New Object[] { }); Type NT=Navigator. GetType (); Objectuseragent = NT. InvokeMember ("useragent", BindingFlags.GetProperty,NULL, navigator,New Object[] { }); returnuseragent.tostring (); } } }
View Code
Methods for modifying the user-agent of the WebBrowser control in WinForm (successfully tested)
Methods for modifying the user-agent of the WebBrowser control in WinForm (successfully tested)