Get Response Header/Body
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 htt开发者_如何学编程pObserver =
{
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.
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/
精彩评论