如题,我用C#封装了webbrowser控件,但不知道如何修改UserAgent,求具体的实现方法!

解决方案 »

  1.   

    public partial class Form1 : Form{    bool isUserAgentSet = false;
        public Form1()    {        InitializeComponent();    }
        private void Form1_Load(object sender, EventArgs e)    {        this.webBrowser.Navigate(“Http://someurl.com”);    }
        private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)    {        //Check if the custom user agent is set        if (!isUserAgentSet)        {            //cancel the current request            e.Cancel = true;            this.isUserAgentSet = true;
                //Navigate to the desired location using the custom user agent            this.webBrowser.Navigate(e.Url, e.TargetFrameName, null, "User-Agent: CustomUserAgent\r\n");        }    }
        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)    {        //reset isUserAgentSet to prepare for the next navigate command        this.isUserAgentSet = false;    }} 
      

  2.   

    那在Navigating如何判断是否是iframe里加载的文档呢,上面的代码,如果HTML含有iframe,就会产生非预期的效果,如何解决呢?
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Reflection;namespace WindowsFormsApplication2
    {
    public class UserAgentHelper
    {
        private static string defaultUserAgent = null;    [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;    /// <summary>
        /// 在默认的UserAgent后面加一部分
        /// </summary>
        public static void AppendUserAgent(string appendUserAgent)
        {
          if (string.IsNullOrEmpty(defaultUserAgent))
            defaultUserAgent = GetDefaultUserAgent();      string ua = defaultUserAgent + ";" + appendUserAgent;
          ChangeUserAgent(ua);
        }    /// <summary>
        /// 修改UserAgent
        /// </summary>
        public static void ChangeUserAgent(string userAgent)
        {
          UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, userAgent, userAgent.Length, 0);
        }    /// <summary>
        /// 一个很BT的获取IE默认UserAgent的方法
        /// </summary>
        private static string GetDefaultUserAgent()
        {
          WebBrowser wb = new WebBrowser();
          wb.Navigate("about:blank");
          while (wb.IsBusy) Application.DoEvents();
          object window = wb.Document.Window.DomWindow;
          Type wt = window.GetType();
          object navigator = wt.InvokeMember("navigator", BindingFlags.GetProperty,
              null, window, new object[] { });
          Type nt = navigator.GetType();
          object userAgent = nt.InvokeMember("userAgent", BindingFlags.GetProperty,
              null, navigator, new object[] { });
          return userAgent.ToString();
        }
    }
    }
     
      

  4.   

    http://hi.baidu.com/prestohuan/blog/item/9f15b0519311f811367abe44.html
      

  5.   


    这个方法跟1楼是一样的,无法解决iframe的问题,希望高手能给出指点!
      

  6.   

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