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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论