修改firefox扩展中的请求的发布数据

时间:2011-01-25 09:06:43

标签: http firefox firefox-addon xmlhttprequest

我正在尝试捕获http请求,更改其一些post参数并发送修改后的请求。 我尝试使用上传流的setData方法来修改请求,但是发送了相同的原始请求。

我在“http-on-modify-request”上执行以下代码:

//rewind the request to read post body  
channel= subject.QueryInterface(Components.interfaces.nsIHttpChannel);
channel=channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
channel = channel.uploadStream;  
channel.QueryInterface(Components.interfaces.nsISeekableStream)
                .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
var stream = Components.classes["@mozilla.org/binaryinputstream;1"]
                .createInstance(Components.interfaces.nsIBinaryInputStream);  
stream.setInputStream(channel);  
var postBytes = stream.readByteArray(stream.available());  
poststr = String.fromCharCode.apply(null, postBytes);  

//change the poststr

poststr=poststr.replace(....);  
stringStream.setData(poststr, poststr.length);  
//changing the postdata  
channel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);  
channel = channel.uploadStream;  
channel = channel.QueryInterface(Components.interfaces.nsISeekableStream)
          .seek(Components.interfaces.nsISeekableStream.NS_SEEK_SET, 0);  
channel.uploadStream.QueryInterface(Components.interfaces.nsIMIMEInputStream);  
channel.uploadStream.setData(stringStream);  
channel.send();

我在这里做错了什么? 我尝试中止初始请求并从一个新的请求开始,但然后页面根本没有加载。 提前完成。

1 个答案:

答案 0 :(得分:0)

嘿,我弄清楚出了什么问题! :)

uploadstream.setData有一个萌芽。 它将通道的请求方法设置为PUT而不是POST 所以我们需要在setData调用之后更改它。 以下似乎解决了这个问题:)

channel.uploadStream.setData(stringStream);   
channel.requestMethod = "POST";  

@ MatrixFrog:你是对的。我不需要致电channel.send。这部分是照顾:)