I'm trying to get the http response headers every time I visit some site. I thought that using an observer like the following is enough to do it:

const OBS = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService); 
let httpRequestObserver ={ 
      observe: function(subject, topic, data){
          var httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
        if (topic == "http-on-examine-response") {
          headers=httpChannel.getAllResponseHeaders();  
        }
      }
    };

And in the startup method I add it then in the shutdown I remove it:

OBS.addObserver(httpRequestObserver, "http-on-examine-response", false);//startup methode

OBS.addObserver(httpRequestObserver, "http-on-examine-response", false);//shutdown

But I'm getting this in the log:

JavaScript Error: "httpChannel.getAllResponseHeaders is not a function"

Am I taking the wrong way and the operation is more complicated than it seem? this is for an extension for firefox for android and i'm not using sdk. Thanks for your help.

有帮助吗?

解决方案

nsIHttpChannel is not XMLHttpRequest. Instead XMLhttpRequest is a nice wrapper class around channels - not just http ones -, which also adds convenience functions such as getAllResponseHeaders().

You may use nsIHttpChannel.visitResponseHeaders to simulate getAllResponseHeaders.

if (subject instanceof Ci.nsIHttpChannel) {
  var headers = "";
  subject.visitResponseHeaders(function(header, value) {
    headers += header + ": " + value + "\r\n";
  });
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top