问题
I just started to use awesomium. I wanted to understand how to change user-agent and referrer. i need for example to initialize 5 istance o awesomium webcontrol and for each of them i need different user-agent and different referrer. this is my simple code
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();
}
these 2 lines give an error
browser.SetHeaderDefinition("MyHeader", myCol);
browser.AddHeaderRewriteRule("http://*", "MyHeader");
Error 1 'Awesomium.Windows.Forms.WebControl' does not contain a definition for 'SetHeaderDefinition' Error 1 'Awesomium.Windows.Forms.WebControl' does not contain a definition for 'AddHeaderRewriteRule'
thanks for the help
回答1:
You need to set the user agent of the WebCore that governs your WebControl. This must be done before you create your WebControl.
public WebForm()
{
if ( !WebCore.IsRunning )
WebCore.Initialize( new WebConfig() { UserAgent = "YourUserAgent" } );
InitializeComponent();
}
回答2:
My solution - is wrapper around WebBrowser, that implements IResourceInterceptor and can navigate with any additional headers:
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;
}
}
来源:https://stackoverflow.com/questions/15992233/awesomium-change-useragent-and-referrer