获取响应头/响应体

Get Response Header/Body

本文关键字:响应 获取      更新时间:2023-09-26

我正在开发一个firefox插件,我正在听这样的http响应:

var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(httpObserver, "http-on-examine-response", false);

调用我的httpObserver对象,看起来像这样:

var httpObserver =
{
 observe : function(aSubject, aTopic, aData) 
 {  
            aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
            httpstatus = aSubject.responseStatus;
            if(httpstatus == 200 && aSubject.contentType.toString().indexOf("text/") != -1)
            {
                alert("URL: " + aSubject.name + "'r'nStatus: " + httpstatus + "'r'nContentType: " + aSubject.contentType); //Works great
                alert("Body: " + aSubject.responseText); //Is undefined, why?
            }
            else if(httpstatus == 404 && aSubject.contentType.toString().indexOf("text/html") != -1)
                alert("Page not found!'r'nURL: " + aSubject.name + "'r'nContentType: " + aSubject.contentType);
 }
};

我有以下问题:

我想从响应中获得整个 body和header,但我不知道如何。我曾经读到过服务器不在同一域中可能是一个问题,但是firefox如何处理它呢?

我已经做了大量的研究,但没有发现任何有用的东西,也许我遗漏了什么。

顺便说一句。:如果有帮助的话,我也可以访问请求对象

您将使用nsITraceableChannel接口,它由所有HTTP请求实现。这允许您用自己的侦听器替换通道侦听器,这样您就可以在将onDataAvailable调用中的数据传递给原始侦听器之前读取数据。下面是一些示例代码:http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/