【问题标题】:Is that possible ? GeckoFX can use seperate CookieContainer per instance?那可能吗 ? GeckoFX 可以为每个实例使用单独的 CookieContainer 吗?
【发布时间】:2013-12-11 03:56:03
【问题描述】:

我正在使用Geckfx22.0xulrunner22.0。由于 .Net 中的 GeckoWebBrowser 与所有其他 GeckoWebBrowser 实例共享 cookie,我希望 GeckoWebBrowser 拥有自己的 cookie 容器,该容器不共享之前在其他 GeckoWebBrowser 或其他实例中创建的任何 cookie。

例如,当我创建 GeckoWebBrowser 时,它不应该有任何 cookie。当我运行 2 个 GeckoWebBrowser 实例时,它们有自己的 cookie 容器,并且不会相互共享或冲突 cookie。

这怎么可能?

我通过创建不同的类和启动geckofx 尝试了各种可能的方法,但是当同时运行不同的浏览器时,它会在其他浏览器之间共享 cookie。如果我从一个浏览器中删除 cookie,其他浏览器也会发生同样的情况。我已经在不同的时间启动了proxyuseragent,它的工作原理,但不能同时为多个浏览器应用各种用户代理。

 public void Initiate()
    {
        Gecko.Xpcom.Initialize(AppDomain.CurrentDomain.BaseDirectory + "/xulrunner");
        if (this.IsProxySet)
        {
            Gecko.GeckoPreferences.User["network.proxy.http"] = this.Host;
            Gecko.GeckoPreferences.User["network.proxy.http_port"] = this.Port;
            Gecko.GeckoPreferences.User["network.proxy.type"] = 1;
        }
        if (IsUseragentSet)
        {
            Gecko.GeckoPreferences.User["general.useragent.override"] = this.Useragent;
        }
    }

为了删除 cookie,我使用以下代码:

nsICookieManager CookieMan;
            CookieMan = Xpcom.GetService<nsICookieManager>("@mozilla.org/cookiemanager;1");
            CookieMan = Xpcom.QueryInterface<nsICookieManager>(CookieMan);
            CookieMan.RemoveAll(); 

我们将不胜感激!!!

【问题讨论】:

    标签: c# winforms cookies geckofx


    【解决方案1】:

    您可以尝试实现自己的支持此功能的 cookie 管理器:

    见单元测试Register_AfterDefaultFactoryHasBeenUnregistered_NewCookieServiceIsUsedInsteadOfDefaultOne 有关如何执行此操作的示例。

    此代码目前未经测试,可能包含 typeos

    此代码需要高于 v22.0-0.6 的 geckofx 版本

    [Guid("c375fa80-150f-11d6-a618-0010a401eb10")]
        [ContractID(TestCookieServiceFactory.ContractID)]
        public class TestCookieServiceFactory
            : GenericOneClassNsFactory<TestCookieServiceFactory, TestCookieService>
        {
            public const string ContractID = "@mozilla.org/cookieService;1";
        }
    
     public class TestCookieService : nsICookieService
     {
       // Implement nsICookieService...       
     }
    
     public void Main()
     {
         Xpcom.Initialize("My Xulrunner/Fireofox location");
         var existingFactoryDetails = TestCookieServiceFactory.Unregister();
         TestCookieServiceFactory.Register();
    
         var browser = new GeckofxWebBrowser();
         // Add browser to form etc...
         browser.Navigate("http://SomeWebPageThatUsesCookies")
    
         // Cookie requests should now be sent to TestCookieService, process them as your want.
     }
    

    【讨论】:

    • 你能解释一下吗?
    • 单元测试演示如何替换 Xpcom 服务 "@mozilla.org/cookieService;1"。 (我怀疑可以为 cookiemanager 做同样的事情;1 或者 cookiemanager 可能通过使用 cookieService 来工作)。它的工作方式类似于 TestCookieServiceFactory 创建一个新的“@mozilla.org/cookieService;1”实现。 (实际工作委托给一个实现nsICookieService接口的TestCookieService类。)然后尽早在程序启动时(即XpCom.Init之后)调用TestCookieServiceFactory.Unregister()去除默认服务,然后调用Register()安装新的。
    • 我找不到TestCookieServiceFactory?如果可能的话,你能给出任何编程例子吗?
    • 我试过了,但它不起作用。无法呼叫Unregister。他们为每个浏览器实例共享相同的数据库文件(如果我们也在不同的项目中使用浏览器实例)来存储和获取 cookie 和缓存。因此,当我们第一次运行项目时,它会在 temp 中创建一个文件夹,并为每个项目使用相同的文件夹。唯一认为我们能做的是,尝试为每个浏览器实例创建不同的数据库。我不知道这是否可能。如果您有任何想法,请告诉我。我正在寻找.....
    • 哎呀抱歉。您需要使用比“v22.0-0.6”更新的 geckofx 版本 - 我目前正在使用从源代码构建的版本。因此,您可以从源代码构建或等待 v22.0-0.7。
    猜你喜欢
    • 2013-08-15
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多