C# GeckoFX内核浏览器清空Cookie的方法

Author:flymorn Source: flymorn
Categories: C#编程 PostTime:2011-6-17 3:27:19
正 文:
   Gecko是一款强大的浏览器内核,著名的Firefox浏览器就是基于 Gecko 核心的。有了 GeckoFX,我们就可以用C#开发一款和 firefox 同样内核的浏览器了。 飘易使用的C#封装的 GeckoFX 版本为 GeckoFX 1.9.1.0。

    闲话少说,直入主题,如何清空 Gecko浏览器产生的 cookies 呢? 直接使用 Skybound 工作室编译好的 Skybound.Gecko.dll 是不行的,因为源码中根本没有封装cookie相关的操作函数。我们需要重新编译 GeckoFX 的源码。

    具体方法如下:

    打开  Xpcom.cs(红色的代码是新增加的):
//Initialize a static variable for the cookie manager
static nsICookieManager CookieMan;

public static void Initialize() 
{
     Initialize(null);
      InitializeExtras();   
}

static void InitializeExtras()
{
    //Initialize the cookie manager
    CookieMan = Xpcom.GetService<nsICookieManager>("@mozilla.org/cookiemanager;1");
    CookieMan = Xpcom.QueryInterface<nsICookieManager>(CookieMan);
}

public static void Initialize(string binDirectory)
{
 .....
  InitializeExtras();
}

public static void DeleteAllCookies()
{
    CookieMan.removeAll();
}

    修改好 Xpcom.cs 后重新编译生成.dll库文件,然后程序里调用新编译过的 Skybound.Gecko.dll库文件。如何调用进行清空cookie?直接在程序中需要清空cookie的地方使用语句: 
Xpcom.DeleteAllCookies();
就可以了。

    在编译dll的过程中,可能发生的错误以及解决方法:
1, nsICookieManager 错误:“找不到类型或命名空间名称“nsICookieManager”(是否缺少 using 指令或程序集引用?)”。
这是由于在接口文件中没有定义 nsICookieManager 接口导致。解决方法:打开 nsInterfaces.cs ,在里面添加该接口(红色代码为新增的):
[Guid("00000000-0000-0000-c000-000000000046"), ComImport]
public interface nsISupports
{
object QueryInterface(ref Guid iid);
int AddRef();
int Release();
}

[Guid("AAAB6710-0F2C-11d5-A53B-0010A401EB10"), ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface nsICookieManager
{
        void removeAll();
        void remove(string aDomain, string aName, string aPath, bool aBlocked);
}

2, nsIDOMNSElement 错误:“找不到类型或命名空间名称“nsIDOMNSElement”(是否缺少 using 指令或程序集引用?)”
这是由于debug的版本不对,解决方法:工程属性-->生成-->配置-->选择  Debug 1.9 或 Release 即可。

    这里也有一篇其他网友写的“曲线救国”的清空cookies的方式: geckofx使用之初步探索:cookies.sqlite,飘易不推荐。

    使用 geckofx 控件需要 xulrunner 运行环境,xulrunner下载地址:
http://releases.mozilla.org/pub/mozilla.org/xulrunner/releases/
http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/
xulrunner介绍: https://developer.mozilla.org/cn/XULRunner
里面有好几个版本,因为 GecKoFX readme 里说  GeckoFX now works best with XULRunner 1.9.1 (Firefox 3.5),所以 飘易下载的是  XULRunner 1.9.1.19 版本。

Geckofx论坛: http://geckofx.org/          
Google code: http://code.google.com/p/geckofx/

补充: GeckoFX Readme英文说明
--------------------------------------
GeckoFX is a .NET wrapper around XULRunner, a runtime based on the same source
code as Firefox.  You can add the control to your windows forms app and use it much the
same way as System.Windows.Forms.WebBrowser.

Since GeckoFX is a wrapper, you need to have the XULRunner runtime somewhere on your
development system (and redistribute it with your application).  GeckoFX now works best
with XULRunner 1.9.1 (Firefox 3.5).

(1) Download XULRunner 1.9.1 from:

http://releases.mozilla.org/pub/mozilla.org/xulrunner/releases/1.9.1.19/runtimes/xulrunner-1.9.1.19.en-US.win32.zip

(2) In your application startup code, call:

Skybound.Gecko.Xpcom.Initialize(xulrunnerPath);

where "xulrunnerPath" is the full path to the location where you extracted the "xulrunner" directory
(containing xul.dll, xpcom.dll, etc).

(3) OPTIONAL: Specify a profile directory by setting Xpcom.ProfileDirectory.

(4) OPTIONAL: There are some files included with XULRunner which GeckoFX doesn't need.  You may
safely delete them:

AccessibleMarshal.dll
dependentlibs.list
mozctl.dll
mozctlx.dll
java*.*
*.ini
*.txt
*.exe

(5) OPTIONAL:  XULRunner does not support about:config out of the box.  If you want to provide
access to this configuration page, copy the files from the "chrome" directory that came with
GeckoFX into the "chrome" directory in your XULRunner path.

The files that need to be copied are "geckofx.jar" and "geckofx.manifest".
--------------------------------------
作者: flymorn
来源: flymorn
版权所有。转载时必须以链接形式注明作者和 原始出处及本声明。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#使用Chrome内核调用后端方法可以通过将C#代码嵌入到Web页面来实现。以下是一种实现方式: 1. 首先,你需要在C#创建一个Web服务器来承载你的页面和后端方法。可以使用ASP.NET 或者其他Web框架来实现。 2. 在你的Web页面,可以使用JavaScript调用后端方法。你可以使用JavaScript的fetch或者XMLHttpRequest来发送HTTP请求到后端,并传递参数。例如: ```javascript fetch('/your-backend-method', { method: 'POST', body: JSON.stringify({ param1: 'value1', param2: 'value2' }), headers: { 'Content-Type': 'application/json' } }) .then(response => response.json()) .then(data => { // 处理后端返回的数据 }); ``` 这里的`/your-backend-method`是后端方法的路由地址,可以根据你的需求进行修改。 3. 在C#的Web服务器,你需要处理这个路由地址的请求,并执行对应的后端方法。可以使用ASP.NET的Web API或者其他方式来处理请求。例如,在ASP.NET Web API,你可以创建一个控制器并添加一个对应的路由: ```csharp [Route("your-backend-method")] [HttpPost] public IActionResult YourBackendMethod([FromBody] YourParameters parameters) { // 执行后端方法逻辑 // 返回数据 } ``` 这里的`YourParameters`是你定义的参数类,用于接收前端传递的参数。 通过以上步骤,你就可以在C#使用Chrome内核调用后端方法了。当JavaScript代码调用后端方法时,会触发对应的请求,C#的Web服务器会接收到请求并执行对应的后端方法逻辑,最后将结果返回给前端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值