更改 WebBrowser 控件的用户代理 [英] Changing the user agent of the WebBrowser control

查看:19
本文介绍了更改 WebBrowser 控件的用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 Winforms 应用程序中 WebBrowser 控件的 UserAgent.

I am trying to change the UserAgent of the WebBrowser control in a Winforms application.

我使用以下代码成功实现了这一点:

I have successfully achieved this by using the following code:

[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;

public void ChangeUserAgent()
{
    List<string> userAgent = new List<string>();
    string ua = "Googlebot/2.1 (+http://www.google.com/bot.html)";

    UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, ua, ua.Length, 0);
}

唯一的问题是这只有效一次.当我第二次尝试运行 ChangeUserAgent() 方法时,它不起作用.它保持设置为第一个更改的值.这很烦人,我已经尝试了所有方法,但它不会改变不止一次.

The only problem is that this only works once. When I try to run the ChangeUserAgent() method for the second time it doesn't work. It stays set to the first changed value. This is quite annoying and I've tried everything but it just won't change more than once.

有人知道一种不同的、更灵活的方法吗?

Does anyone know of a different, more flexible approach?

谢谢

推荐答案

我不确定是否应该从 网站,但我宁愿将答案留在这里,而不是链接.如果有人可以在评论中澄清,我将非常感激.

I'm not sure whether I should just copy/paste from a website, but I'd rather leave the answer here, instead of a link. If anyone can clarify in comments, I'll be much obliged.

基本上,您必须扩展 WebBrowser 类.

Basically, you have to extend the WebBrowser class.

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}
", UserAgent);
                renavigating = true;
                cancel = true;
                Navigate((string)url, (string)targetFrameName, (byte[])postData, (string)headers);
            }
            else
            {
                renavigating = false;
            }
        }
    }
}

注意:要使用上述方法,您需要添加对Microsoft Internet Controls"的 COM 引用.

Note: To use the method above you’ll need to add a COM reference to "Microsoft Internet Controls".

他也提到了你的方法,并指出 WebBrowser 控件似乎缓存了这个用户代理字符串,所以它不会在不重新启动进程的情况下更改用户代理.

He mentions your approach too, and states that the WebBrowser control seems to cache this user agent string, so it will not change the user agent without restarting the process.

这篇关于更改 WebBrowser 控件的用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆