-1

我正在构建一个扩展,我可以在其中捕获所有发布请求。但是在 httpChannel.originalURI.spec 中没有来自帖子的任何属性。我怎样才能获得帖子的属性?

myObserver.prototype = {

 observe: function(subject, topic, data) {

  if("http-on-modify-request"){

    var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);

    if(httpChannel.requestMethod=="POST")
      alert(httpChannel.originalURI.spec);

       }
  }

},
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                      .getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, "http-on-modify-request", false);

},
unregister: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
                        .getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
}
}

有任何想法吗?

4

2 回答 2

0

nsIHttpChannel 仅提供对 HTTP 标头的访问。POST 数据作为请求正文的一部分发送,因此您需要将对象接口更改为 nsIUploadChannel 并将二进制上传数据读入字符串。

var uploadChannel = httpChannel.QueryInterface(Ci.nsIUploadChannel);
var uploadStream = uploadChannel.uploadStream;
uploadStream.QueryInterface(Ci.nsISeekableStream).
             seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
var binStream = Cc["@mozilla.org/binaryinputstream;1"].
                createInstance(Ci.nsIBinaryInputStream);
binStream.setInputStream(uploadStream);
var postBytes = binStream.readByteArray(binStream.available());
var postString = String.fromCharCode.apply(null, postBytes);
于 2013-08-01T19:07:11.617 回答
-1

来自 Luckyrat 的代码对我来说不能正常工作。我不得不处理一些超时的请求。注意到 nmaiers 评论此代码工作正常(据我所知):

function getPostString(httpChannel) {
    var postStr = "";
    try {
        var uploadChannel = httpChannel.QueryInterface(Ci.nsIUploadChannel);
        var uploadChannelStream = uploadChannel.uploadStream;
        if (!(uploadChannelStream instanceof Ci.nsIMultiplexInputStream)) {
            uploadChannelStream.QueryInterface(Ci.nsISeekableStream).seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
            var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
            stream.setInputStream(uploadChannelStream);
            var postBytes = stream.readByteArray(stream.available());

            uploadChannelStream.QueryInterface(Ci.nsISeekableStream).seek(0, 0);

            postStr = String.fromCharCode.apply(null, postBytes);
        }
    }
    catch (e) {
        console.error("Error while reading post string from channel: ", e);
    }
    finally {
        return postStr;
    }
}
于 2014-01-19T02:19:23.443 回答