2011-01-25 46 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。這部分是照顧:)