[C# Discussion] Awesomium /// Change Referrer & USer-Agent

ldg2002

Registered Member
Joined
Jun 5, 2012
Messages
66
Reaction score
6
hi guys, I just started to use awesomium.
I wanted to understand how to change user-agent and referrer , in my bot I have multiple instances of the webcontrol and I wish each of them has a different user-agent and referrer.
thanks for the help
 
Hey man, many thanks, but there's something strange...
this is my code

Code:
private void newbrowser()
        {
                WebConfig config;
                config.referrer = "ref";
                config.useragent = "useragent";
                //^Code completion should help with finding the correct names, but that should be it.
                WebCore.Initialize(config);
                browser = new Awesomium.Windows.Forms.WebControl();
                //delay until control is ready
                browser.Paint += browser_Paint;
                tabPage1.Controls.Add(browser);
                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;
                
        }
i've this 2 errors:
Error 1 'Awesomium.Core.WebConfig' does not contain a definition for 'referrer' and no extension method 'referrer' accepting a first argument of type 'Awesomium.Core.WebConfig' could be found (are you missing a using directive or an assembly reference?) C:\Users\xxx\Documents\Visual Studio 2010\Projects\xxx\xxx\Form1.cs 40 24 xxx
Error 2 'Awesomium.Core.WebConfig' does not contain a definition for 'useragent' and no extension method 'useragent' accepting a first argument of type 'Awesomium.Core.WebConfig' could be found (are you missing a using directive or an assembly reference?) C:\Users\xxx\Documents\Visual Studio 2010\Projects\xxx\xxx\Form1.cs 41 24 xxx
 
Hey man, many thanks, but there's something strange...
this is my code

Code:
private void newbrowser()
        {
                WebConfig config;
                config.referrer = "ref";
                config.useragent = "useragent";
                //^Code completion should help with finding the correct names, but that should be it.
                WebCore.Initialize(config);
                browser = new Awesomium.Windows.Forms.WebControl();
                //delay until control is ready
                browser.Paint += browser_Paint;
                tabPage1.Controls.Add(browser);
                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;
                
        }
i've this 2 errors:
Error 1 'Awesomium.Core.WebConfig' does not contain a definition for 'referrer' and no extension method 'referrer' accepting a first argument of type 'Awesomium.Core.WebConfig' could be found (are you missing a using directive or an assembly reference?) C:\Users\xxx\Documents\Visual Studio 2010\Projects\xxx\xxx\Form1.cs 40 24 xxx
Error 2 'Awesomium.Core.WebConfig' does not contain a definition for 'useragent' and no extension method 'useragent' accepting a first argument of type 'Awesomium.Core.WebConfig' could be found (are you missing a using directive or an assembly reference?) C:\Users\xxx\Documents\Visual Studio 2010\Projects\xxx\xxx\Form1.cs 41 24 xxx

Ah, guess I went wrong somewhere.

Check this out: http://stackoverflow.com/questions/...e-with-enable-control-in-winform-c-sharp?rq=1
Should be helpful, adding the useragent on that would be like myCol.Add("UserAgent","useragent"); (right after setting the referrer with myCol.Add).
 
Man thsnk you again...
but i've another 2 errors.. :confused:


Code:
browser.[COLOR=#2B91AF]SetHeaderDefinition[/COLOR]([COLOR=#800000]"MyHeader"[/COLOR], myCol); 
       browser.[COLOR=#2B91AF]AddHeaderRewriteRule[/COLOR]([COLOR=#800000]"http://*"[/COLOR], [COLOR=#800000]"MyHeader"[/COLOR]);

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'

thank you for your patience
 
Man thsnk you again...
but i've another 2 errors.. :confused:


Code:
browser.[COLOR=#2B91AF]SetHeaderDefinition[/COLOR]([COLOR=#800000]"MyHeader"[/COLOR], myCol); 
       browser.[COLOR=#2B91AF]AddHeaderRewriteRule[/COLOR]([COLOR=#800000]"http://*"[/COLOR], [COLOR=#800000]"MyHeader"[/COLOR]);

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'

thank you for your patience
What version are you using?
 
Well I suggest you to use xulrunner!
hey capripio, thank you for the reply, i use c#, xulrunner can run in c#?
i need something simple that can run for example 5 webrower each with different useragent and different referrer
thank you!
 
hey capripio, thank you for the reply, i use c#, xulrunner can run in c#?
i need something simple that can run for example 5 webrower each with different useragent and different referrer
thank you!

Yes You can do this+plus you can use proxies aswell!
 
I'm trying with geckofx :)
 
You can do it with 1.7, you have to use the resourceintercepter callback.

Tbh I think it was better the way before, as depending on your code and what you are wanting to do, it may be difficult for you to set the properties.

but anyway this is how to do it

Code:
internal class ResourceInterceptor : IResourceInterceptor
{
   
    public bool OnFilterNavigation(NavigationRequest request)
    {
        return false;
    }

    ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request)
    {
        if (request == null) return null;
        request.AppendExtraHeader("User-Agent","ua here" );
        request.Referrer = "referrer here"; 
    }
}

then when you start your webcore

Code:
void StartWebCore(){
    ...
    WebCore.Started += WebCoreOnStarted;
}

static void WebCoreOnStarted(object sender, CoreStartEventArgs coreStartEventArgs)
{
    var interceptor = new ResourceInterceptor();
    WebCore.ResourceInterceptor = interceptor;
}
 
hey rootjazz, thank you for the help, i've got this error

Error 1 'WindowsFormsApplication1.Form1.ResourceInterceptor.Awesomium.Core.IResourceInterceptor.OnRequest(Awesomium.Core.ResourceRequest)': not all code paths return a value

on this
Code:
ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request)
            {
                if (request == null) return null;
                request.AppendExtraHeader("User-Agent", "ua here");
                request.Referrer = "referrer here";


            }
 
hey rootjazz, thank you for the help, i've got this error

Error 1 'WindowsFormsApplication1.Form1.ResourceInterceptor.Awesomium.Core.IResourceInterceptor.OnRequest(Awesomium.Core.ResourceRequest)': not all code paths return a value

on this
Code:
ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request)
            {
                if (request == null) return null;
                request.AppendExtraHeader("User-Agent", "ua here");
                request.Referrer = "referrer here";


            }

Sorry buddy, I removed some of my other code there and removed the return, you should include
Code:
  return null;

so your method would be
Code:
ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request)
            {
                if (request == null) return null;
                request.AppendExtraHeader("User-Agent", "ua here");
                request.Referrer = "referrer here";

                 return null;
            }
 
Also, not sure if this bug was unique to me due to some other things I did, but be careful if you try and access (not use, just access) the webview.Identifier.

If I just tried to store that to a variable, the WebCore would just shutdown after 1-2 webviews had pulled a page. Took hours to figure out and almost had me throwing the laptop out the window. If you need to store a way to identify the webview for accessing in the
Code:
ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request) {
   request.webview
...

then only way I could do it is a Dictionary using the full webview as key. Bit of an overkill, but I guess the Identifier, as it is a new feature for 1.7 is still a bit buggy.
 
this one works!
Code:
        internal class ResourceInterceptor : IResourceInterceptor        {


            public bool OnFilterNavigation(NavigationRequest request)
            {
                return false;
            }


            ResourceResponse IResourceInterceptor.OnRequest(ResourceRequest request)
            {
                if (request.Method != "GET")
                {
                    return null;
                }


                request.AppendExtraHeader("User-Agent", useragentok());
                request.Referrer = "http://awesomium.com";


                return null;
            }
        }

but i've this error
Error 6 Cannot access a non-static member of outer type 'WindowsFormsApplication1.Form1' via nested type 'WindowsFormsApplication1.Form1.ResourceInterceptor'

seem that i can't use a variable in request.AppendExtraHeader("User-Agent", useragentok());
 
All awesomium interaction must be done on the same thread, this could be something to check.

Depending what you doing, I believe there is a syncronisation object you can use (for a webbrowser control). If you are going headless you will need to handle this yourself.

Or perhaps your objects haven't initialised yet?
 
Back
Top