获取响应标头/正文
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
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 inonDataAvailable
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/