我建立一个扩展程序,我抓住所有的帖子请求。但是在httpChannel.originalURI.spec中,帖子中没有任何属性。我如何获得帖子的atrtibutes?
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");
}
}
有什么想法吗?
先谢谢。
答案 0 :(得分: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);
答案 1 :(得分:0)
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;
}
}