2011-01-25 5 views
1

httpリクエストをトラップし、ポストパラメータの一部を変更して変更されたリクエストを送信しようとしています。 アップロードストリームの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

+0

http-on-modify-requestから 'channel.send'を明示的に呼び出す必要はありません.Firefoxはあなたのコードを呼び出した後にそれを行います。あなたはそれを取って、それが何か変わるかどうかを調べることができます。 – MatrixFrog

答えて

0

ねえ、私は何が間違っているのか考え出しました! :)

uploadstream.setDataにはバグがあります。 これは、POSTのコードではなく、 の代わりに、チャンネルのリクエストメソッドをPUTに設定します。そのため、setData呼び出しの後に変更する必要があります。 次は、問題を解決するようだ:)

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

:あなたは正しいです。 channel.sendに電話する必要はありません。その部分は世話をしています:)

関連する問題