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

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

问题描述

我试图改变一个WinForms应用程序WebBrowser控件的用户代理。

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

我已经成功地通过以下code做到了这一点:

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. I 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?

感谢

推荐答案

我不知道是否我应该只是复制/粘贴从<一个href=\"http://wallerdev.com/2008/12/25/changing-the-user-agent-in-ie-net-webbrowser-control-via-csharp\">website,但我宁愿离开这里找答案,而不是链接。如果任何人都可以在留言澄清,我将非常感激。

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

请注意:要使用上面的方法,您需要COM引用添加到Microsoft Internet控制

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天全站免登陆