2011-01-25 50 views
1

我试图陷入一个http请求,改变它的一些post参数并发送修改后的请求。 我尝试使用上传流的setData方法来修改请求,但发送了相同的原始请求。修改firefox扩展中的请求的发布数据

我有下面的代码执行在“HTTP上 - 修改 - 请求”:

//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(); 

我在做什么错在这里? 我试图放弃最初的请求,并从一个新的请求开始,但然后页面根本不加载。 Thanx提前。

+0

你可以使用`{}`按钮来让你的代码看起来像代码时,问一个问题。 – MatrixFrog 2011-02-01 01:10:38

回答

0

嘿我想出了什么是错的! :)

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

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

@ MatrixFrog:你说得对。我不需要拨打channel.send。这部分是照顾:)