扩展firefox中的Post标头
Post header in extension firefox
我正在构建一个扩展,在这里我可以捕获所有的post请求。但是在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");
}
}
有什么想法吗?
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);
Luckyrat的代码对我来说工作不正常。我不得不处理一些超时的请求。注意到nmairs注释此代码工作正常(据我所知):
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;
}
}
相关文章:
- YUI3 IO实用程序是否可以根据给定的内容类型标头值自动序列化数据
- 如何编写一个具有公共标头的批处理
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 如何使用自定义标头跨域执行AJAX POST
- 在Http Post上添加授权标头
- Angularjs Post未向Spring JWT发送标头
- Angular JS和Restangular:如何在POST请求中包含标头和参数
- 如何确保标头 cookie 在 frisby.js 中的 post 方法上传递
- 在 POST 后使用 Location 标头和 jQuery 重定向
- POST 请求标头中的内容类型中缺少表单数据边界
- 如何读取空响应正文的 ajax post 调用的响应标头
- 如何使用jQuery在点击时修改POST标头
- jquery post:否'访问控制允许来源'标头存在于请求的资源上
- 扩展firefox中的Post标头
- 在POST请求期间,无法在IE 8中的ajax请求标头上设置内容类型
- 如何在Javascript POST中控制标头属性
- CORS POST请求在授权标头上抛出
- XHR POST无法发送凭据和标头
- AngularJS:不能发送带有适当CORS标头的POST请求
- 如何在知道其标头和POLL响应的情况下进行POST查询