nsIUploadChannel - 连接已重置

发布于 2024-12-03 17:17:05 字数 2088 浏览 0 评论 0原文

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

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

这是我的代码:

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

我做错了什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2024-12-10 17:17:05

由于您正在从上传流中读取数据并将其写入文件,因此您可能会将流指针保留在末尾(因此当请求发生时,POST 正文看起来像是空的)。读出数据后,尝试返回流的开头。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文