Awesomium,更改useragent和referrer

时间:2013-04-13 19:54:22

标签: c# user-agent referrer awesomium

我刚刚开始使用awesomium。 我想了解如何更改用户代理和引荐来源。 我需要例如初始化5 istance o awesomium webcontrol,并为他们每个我需要不同的用户代理和不同的推荐人。 这是我的简单代码

        private void newbrowser()
    {
            browser = new Awesomium.Windows.Forms.WebControl();
            browser.Paint += browser_Paint;
            browser.Location = new System.Drawing.Point(1, 1);
            browser.Name = "webControl";
            browser.Size = new System.Drawing.Size(1024, 768);
            browser.Source = new System.Uri("https://www.google.com/", System.UriKind.Absolute);
            browser.TabIndex = 0;

    }

    void browser_Paint(object sender, PaintEventArgs e)
    {
        browser.Paint -= browser_Paint;

        System.Collections.Specialized.NameValueCollection myCol =
            new System.Collections.Specialized.NameValueCollection();
        myCol.Add("Referer", "http://www.yahoo.com");

        browser.SetHeaderDefinition("MyHeader", myCol);
        browser.AddHeaderRewriteRule("http://*", "MyHeader");
    }
    private void button1_Click(object sender, EventArgs e)
    {
        newbrowser();
    }

这两行给出错误

        browser.SetHeaderDefinition("MyHeader", myCol);
        browser.AddHeaderRewriteRule("http://*", "MyHeader");
  

错误1'Awesomium.Windows.Forms.WebControl'不包含   'SetHeaderDefinition'的定义   错误1'Awesomium.Windows.Forms.WebControl'不包含   'AddHeaderRewriteRule'的定义

感谢您的帮助

2 个答案:

答案 0 :(得分:7)

您需要设置管理WebControl的WebCore的用户代理。必须在创建WebControl之前完成此操作。

public WebForm()
{
    if ( !WebCore.IsRunning )
        WebCore.Initialize( new WebConfig() { UserAgent = "YourUserAgent" } );

    InitializeComponent();
}

答案 1 :(得分:1)

我的解决方案 - 是WebBrowser的包装器,它实现了IResourceInterceptor,并且可以使用任何其他头文件进行导航:

public class class BrowserWrapper : IResourceInterceptor
{
    public BrowserWrapper()
    {
        WebCore.ResourceInterceptor = this;
        //BrowserWrapper can contains WebBrowser or knows how to delegate him Naviagtion (Source property)
    }

    private readonly ConcurrentDictionary<Uri, string> headers = new ConcurrentDictionary<Uri, string>();

    public void Navigate(Uri uri, string additionalHeaders = null)
    {
        if (additionalHeaders != null) headers.AddOrUpdate(uri, additionalHeaders, (url, h) => h);
        //Navigation to browser (WebControl.Source = uri...)
    }

    ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request)
    {
        string h;
        if (headers.TryRemove(request.Url, out h))
        {
            var hs = h.Split(':');
            request.AppendExtraHeader(hs[0], hs[1]);
        }
        return null;
    }

    bool IResourceInterceptor.OnFilterNavigation(NavigationRequest request)
    {
        return false;
    }
}