开发者

nsIUploadChannel - The connection was reset

I try to save post data to local file. But if i do that, in browser i see

The connection was reset The connection to the server was reset while the page was loading. The site could be temporarily 开发者_C百科unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer's network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

There is my code:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="yourwindow" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

 <script>
<![CDATA[
var MIKI =
{
 observe: function(subject, topic, data) 
 {
  if (topic == "http-on-modify-request") 
  {
   var httpChannel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
   if(httpChannel.requestMethod == "POST"){
   var uploadChannel = httpChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
   var uploadChannelStream = uploadChannel.uploadStream;
   uploadChannelStream.QueryInterface(Components.interfaces.nsISeekableStream);                 
   uploadChannelStream.seek(0,0);                               
   var stream = Components.classes['@mozilla.org/scriptableinputstream;1'].createInstance(Components.interfaces.nsIScriptableInputStream);
   stream.init(uploadChannelStream);
   var data = stream.read(stream.available());
   data = data.split(/\r\n\r\n/).slice(1).join('\r\n\r\n');
   var buffer = httpChannel.URI.spec + " " + data+ "\n";
   if(data != '') fos.write(buffer, buffer.length);
  }
 }
} 
};
var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("Desk", Components.interfaces.nsIFile);
file.append("http-request-log.txt");
var fos = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
fos.init(file, 0x02 | 0x08 | 0x10, -1, 0); 
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(MIKI, "http-on-modify-request", false);    
]]>
</script>
</window>

What i do wrong?


Since you are reading the data out of the upload stream to write it to the file, you are probably leaving the stream pointer at the end (so when the request happens it looks like the POST body is empty). Try seeking back to the beginning of the stream once you are done reading out the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜