nsIUploadChannel - 重置了连接

时间:2011-09-06 15:47:48

标签: firefox firefox-addon

我尝试将发布数据保存到本地文件。但如果我这样做,在浏览器中我看到

  

连接已重置             页面时重置了与服务器的连接   加载。该网站可能暂时不可用或太忙。尝试   再一次       时刻。如果您无法加载任何页面,请检查您的   电脑的网络       连接。如果您的计算机或网络受到保护   防火墙或代理,请确保       允许Firefox访问Web。

有我的代码:

<?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>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

由于您正在从上传流中读取数据以将其写入文件,因此您可能将流指针留在最后(因此,当请求发生时,它看起来像POST主体是空的)。读完数据后,尝试回到流的开头。