获取响应标头/正文

发布于 2024-12-09 01:46:40 字数 1277 浏览 0 评论 0原文

我正在开发一个 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);
 }
};

我遇到了以下问题:

我想获取整个 响应中的正文和标题,但我不知道如何。 我曾经读到,服务器不在同一个域中可能是一个问题,但是firefox如何处理它呢?

我已经做了很多研究,但没有发现任何有用的东西,也许我错过了一些东西..

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

I'm developing a firefox addon and I'm listening to http responses like this:

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

Which calls my httpObserver object that looks like this:

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

I've got the following problem(s):

I'd like to get the whole body and header from the response but I don't know how.
I once read that it could be a problem that the server is not in the same domain, but how does the firefox handles it then?

I already did a lot of research but I didn't find anything usefull, maybe I missed something..

Btw.: I've got access to the request object as well if that helps.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-12-16 01:46:40

您可以使用 nsITraceableChannel 接口,它是由所有 HTTP 请求。这允许您用自己的通道侦听器替换通道侦听器,以便您可以在将 onDataAvailable 调用中读取数据,然后再将其传递到原始侦听器。这里有一些示例代码就是这样做的: http://www. softwareihard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/

You would use nsITraceableChannel interface for that, it is implemented by all HTTP requests. This allows you to replace the channel listener by one of your own, so you can read out the data in onDataAvailable call before passing it on to the original listener. There is some example code doing just that here: http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/

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