locked
Change the User Agent String in Internet Explorer 9 RRS feed

  • Question

  • how to change or make addition to the User Agent String in Internet Explorer 9 ?

    Pavan


    • Edited by PavanModi Tuesday, May 29, 2012 10:11 AM
    Tuesday, May 29, 2012 10:10 AM

Answers

  • Hi PavanModi,

      You could extend the functionality to Webbrowser control via the following snippet of code:

     
    public class ExtendedWebBrowser : WebBrowser 
    { 
        bool renavigating = false; 
     
        public string UserAgent { get; set; } 
     
        public ExtendedWebBrowser() 
        { 
            DocumentCompleted += SetupBrowser; 
     
            //this will cause SetupBrowser to run (we need a document object) 
            Navigate("about:blank"); 
        } 
     
        void SetupBrowser(object sender, WebBrowserDocumentCompletedEventArgs e) 
        { 
            DocumentCompleted -= SetupBrowser; 
            SHDocVw.WebBrowser xBrowser = (SHDocVw.WebBrowser)ActiveXInstance; 
            xBrowser.BeforeNavigate2 += BeforeNavigate; 
            DocumentCompleted += PageLoaded; 
        } 
     
        void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e) 
        { 
     
        } 
     
        void BeforeNavigate(object pDisp, ref object url, ref object flags, ref object targetFrameName, 
            ref object postData, ref object headers, ref bool cancel) 
        { 
            if (!string.IsNullOrEmpty(UserAgent)) 
            { 
                if (!renavigating) 
                { 
                    headers += string.Format("User-Agent: {0}\r\n", UserAgent); 
                    renavigating = true; 
                    cancel = true; 
                    Navigate((string)url, (string)targetFrameName, (byte[])postData, (string)headers); 
                } 
                else 
                { 
                    renavigating = false; 
                } 
            } 
        } 
    } 
    

      Meanwhile, you could find its detailed information at this link:

      http://stackoverflow.com/questions/937573/changing-the-useragent-of-the-webbrowser-control-winforms-c-sharp

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 1, 2012 8:19 AM
    Moderator

All replies

  • how to change or make addition to the User Agent String in Internet Explorer 9 ?

    Pavan


    you can try with the following uisng PInvoke

    http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/0e73f734-b1da-4275-9b2c-1379e5a19543


    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    • Proposed as answer by MusicDemon Tuesday, May 29, 2012 10:56 AM
    Tuesday, May 29, 2012 10:44 AM
  • Hi PavanModi,

      If you use the Windows Form control looks like Webbrowse,here is a good way to change the proxy with InternetSetOption method from the wininet.dll, the following code snippet is a example to set the proxy:

     
    using System.Runtime.InteropServices;
    
    Public struct Struct_INTERNET_PROXY_INFO
    {
    public int dwAccessType;
    public IntPtr proxy;
    public IntPtr proxyBypass;
    };
    [DllImport("wininet.dll", SetLastError = true)]
    private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
    
    private void RefreshIESettings(string strProxy)
    {
    const int INTERNET_OPTION_PROXY = 38;
    const int INTERNET_OPEN_TYPE_PROXY = 3;
    
    Struct_INTERNET_PROXY_INFO struct_IPI;
    
    // Filling in structure
    struct_IPI.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
    struct_IPI.proxy = Marshal.StringToHGlobalAnsi(strProxy);
    struct_IPI.proxyBypass = Marshal.StringToHGlobalAnsi("local");
    
    // Allocating memory
    IntPtr intptrStruct = Marshal.AllocCoTaskMem(Marshal.SizeOf(struct_IPI));
    
    // Converting structure to IntPtr
    Marshal.StructureToPtr(struct_IPI, intptrStruct, true);
    
    bool iReturn = InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY, intptrStruct, Marshal.SizeOf(struct_IPI));
    }
    
    private void SomeFunc()
    {
    RefreshIESettings("192.168.1.200:1010");
    
    System.Object nullObject = 0;
    string strTemp = String.Empty;
    System.Object nullObjStr = strTemp;
    axWebBrowser1.Navigate("http://willstay.tripod.com", ref nullObject, ref nullObjStr, ref nullObjStr, ref nullObjStr);
    }

      InternetSetOption inside of unmangered API named wininet.dll is the key point to how to change its web proxy. The snippet of code appears in c++ first,but now it could be migrated into c# code using Pinovoke technolgy.

      Hope it helps you.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 30, 2012 5:50 AM
    Moderator
  • okie Jason,

    But, how to change User Agent String in Internet Explorer 9 programmatically using c#?

    I want to select different USER Agent before launching IE so that it emulates and behaves like user string entered or selected.

    in other words i like to implement same as http://www.howtogeek.com/howto/18450/change-the-user-agent-string-in-internet-explorer-8/ but in c# not in IE..


    Pavan




    • Edited by PavanModi Wednesday, May 30, 2012 7:11 AM
    Wednesday, May 30, 2012 6:39 AM
  • Hi PavanModi,

      You could extend the functionality to Webbrowser control via the following snippet of code:

     
    public class ExtendedWebBrowser : WebBrowser 
    { 
        bool renavigating = false; 
     
        public string UserAgent { get; set; } 
     
        public ExtendedWebBrowser() 
        { 
            DocumentCompleted += SetupBrowser; 
     
            //this will cause SetupBrowser to run (we need a document object) 
            Navigate("about:blank"); 
        } 
     
        void SetupBrowser(object sender, WebBrowserDocumentCompletedEventArgs e) 
        { 
            DocumentCompleted -= SetupBrowser; 
            SHDocVw.WebBrowser xBrowser = (SHDocVw.WebBrowser)ActiveXInstance; 
            xBrowser.BeforeNavigate2 += BeforeNavigate; 
            DocumentCompleted += PageLoaded; 
        } 
     
        void PageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e) 
        { 
     
        } 
     
        void BeforeNavigate(object pDisp, ref object url, ref object flags, ref object targetFrameName, 
            ref object postData, ref object headers, ref bool cancel) 
        { 
            if (!string.IsNullOrEmpty(UserAgent)) 
            { 
                if (!renavigating) 
                { 
                    headers += string.Format("User-Agent: {0}\r\n", UserAgent); 
                    renavigating = true; 
                    cancel = true; 
                    Navigate((string)url, (string)targetFrameName, (byte[])postData, (string)headers); 
                } 
                else 
                { 
                    renavigating = false; 
                } 
            } 
        } 
    } 
    

      Meanwhile, you could find its detailed information at this link:

      http://stackoverflow.com/questions/937573/changing-the-useragent-of-the-webbrowser-control-winforms-c-sharp

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 1, 2012 8:19 AM
    Moderator