modify the post data of a request in firefox extension

谁都会走 提交于 2019-12-23 02:53:09

问题


I am trying to trap an http request, change some of its post parameters and send the modified request. I tried using the setData method of upload stream to modify the request, but the same original request is sent.

I have the following code execute on the "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();

What am I doing wrong here? I tried aborting the initial request and starting with a fresh request, but then the page doesn't load at all. Thanx in advance.


回答1:


hey I figured out what was wrong!! :)

The uploadstream.setData has a bud. It sets the request method of the channel to PUT as opposed to POST So we need to change that after the setData call. The following seems to solve the problem :)

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

@ MatrixFrog : You are right. I don't need to call channel.send. That part is taken care of :)



来源:https://stackoverflow.com/questions/4791598/modify-the-post-data-of-a-request-in-firefox-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!