Monitoring HTTP activity 编辑

Gecko includes the nsIHttpActivityObserver interface, which you can implement in your code to monitor HTTP transactions in real time, receiving a callback as the transactions take place.

Creating an HTTP activity observer

To observe HTTP activity, you need to implement the nsIHttpActivityObserver interface within your code. This is very simple, requiring you to implement a single method, nsIHttpActivityObserver.observeActivity(), which gets called each time an action of interest takes place on the HTTP channel.

// Define a reference to the interface
var nsIHttpActivityObserver = Components.interfaces.nsIHttpActivityObserver; var httpObserver = { observeActivity: function(aHttpChannel, aActivityType, aActivitySubtype, aTimestamp, aExtraSizeData, aExtraStringData) {       if (aActivityType == nsIHttpActivityObserver.ACTIVITY_TYPE_HTTP_TRANSACTION) {         switch(aActivitySubtype) {           case nsIHttpActivityObserver.ACTIVITY_SUBTYPE_RESPONSE_HEADER:             // received response header             break;           case nsIHttpActivityObserver.ACTIVITY_SUBTYPE_RESPONSE_COMPLETE:             // received complete HTTP response             break;         }       } } };

Then you need to install your activity observer. This is done using the nsIHttpActivityDistributor.addObserver() method in nsIHttpActivityDistributor:

var activityDistributor = Components.classes["@mozilla.org/network/http-activity-distributor;1"]
                                    .getService(Components.interfaces.nsIHttpActivityDistributor);
activityDistributor.addObserver(httpObserver);

Observable activities

There are two classes of observable activities: those that occur at the socket level and those that occur at the HTTP transaction level.

Observable socket activities

When the activity type reported to your nsIHttpActivityObserver.observeActivity() method is ACTIVITY_TYPE_SOCKET_TRANSPORT, the activity subtype, which indicates the specific type of activity that occurred, will be a socket transport status code.

Observable HTTP activities

When the activity type is ACTIVITY_TYPE_HTTP_TRANSACTION, the activity subtype will be one of the activity subtype constants. These include the ability to monitor outgoing HTTP request headers and bodies as well as incoming HTTP headers and complete HTTP transactions.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:52 次

字数:4594

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文