1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'C, C++, C#' started by ldg2002, Apr 13, 2013.

  1. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    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
     
  2. Herion

    Herion Jr. VIP Jr. VIP Premium Member

    Joined:
    Jul 8, 2012
    Messages:
    287
    Likes Received:
    72
    Code:
    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);
    
     
    • Thanks Thanks x 1
  3. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    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
     
  4. Herion

    Herion Jr. VIP Jr. VIP Premium Member

    Joined:
    Jul 8, 2012
    Messages:
    287
    Likes Received:
    72
    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).
     
  5. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    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
     
  6. Herion

    Herion Jr. VIP Jr. VIP Premium Member

    Joined:
    Jul 8, 2012
    Messages:
    287
    Likes Received:
    72
    What version are you using?
     
  7. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    1.7.0.5
     
  8. capripio

    capripio Regular Member

    Joined:
    Dec 25, 2010
    Messages:
    248
    Likes Received:
    157
    Occupation:
    Programmer
    Location:
    127.0.0.1
    Well I suggest you to use xulrunner!
     
  9. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    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!
     
  10. capripio

    capripio Regular Member

    Joined:
    Dec 25, 2010
    Messages:
    248
    Likes Received:
    157
    Occupation:
    Programmer
    Location:
    127.0.0.1
    Yes You can do this+plus you can use proxies aswell!
     
  11. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    I'm trying with geckofx :)
     
  12. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    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;
    } 
    
    
     
  13. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    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";
    
    
                }
     
  14. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    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;
                }
     
  15. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    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.
     
  16. ldg2002

    ldg2002 Registered Member

    Joined:
    Jun 5, 2012
    Messages:
    66
    Likes Received:
    6
    Home Page:
    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());
     
  17. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    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?
     
  18. balooka

    balooka Registered Member

    Joined:
    Jan 18, 2009
    Messages:
    92
    Likes Received:
    26
    did you ever make this work