Is that possible ? GeckoFX can use seperate CookieContainer per instance?

大城市里の小女人 提交于 2020-01-03 14:17:11

问题


I'm Using Geckfx22.0 and xulrunner22.0. Since GeckoWebBrowser in .Net shares cookies with all other instances of GeckoWebBrowsers I would like for a GeckoWebBrowser to have it's own cookie container which doesn't share any cookies that was created previously in other GeckoWebBrowsers or other instances.

For example when I create a GeckoWebBrowser it shouldn't have any cookies. And when I run 2 instances of GeckoWebBrowser they have their own cookie container and don't share or conflict cookies with each other.

How is that possible?

I've tried various possible ways by creating different class and initiating geckofx but when running different browser at same time it sharing cookies among other browsers. If i remove cookies from one browser , the same happening for other browsers too. I have initiated the proxy and useragent at different times and its works but cant apply various useragents for multiple browsers at the same time.

 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;
        }
    }

And to remove cookies i'm using following code :

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

Help will be appreciated !!!


回答1:


You could possibly try implementing your own cookie manager that supports this:

see unittest Register_AfterDefaultFactoryHasBeenUnregistered_NewCookieServiceIsUsedInsteadOfDefaultOne for an example of how to do this.

This code is currently untested and may contain typeos

This code requires a geckofx version newer than v22.0-0.6

[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.
 }


来源:https://stackoverflow.com/questions/20209756/is-that-possible-geckofx-can-use-seperate-cookiecontainer-per-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!