【问题标题】:WebClient is stripping newlines from files when using DownloadFile使用 DownloadFile 时,WebClient 正在从文件中删除换行符
【发布时间】:2014-01-01 22:57:58
【问题描述】:

使用此代码下载任何类型的纯文本文件(cpp、txt 等),WebClient 会从文本文件中删除换行符。有没有办法强制 WebClient 将文本文件视为二进制文件?我正在使用同一个 WebClient 下载许多其他文件,其中大多数不是文本,只是一些。

    private void button3_Click_1(object sender, EventArgs e)
    {
        using (var wc = new GZipWebClient())
        {
            wc.Headers["Accept-Encoding"] = "gzip,deflate";
            wc.DownloadFile(@"link", "test.txt");
        }
    }

    class GZipWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            return request;
        }
    }

【问题讨论】:

  • the WebClient is stripping the newlines out of the text file 不,它没有。
  • 我会惊讶发现它正在这样做。我的猜测是原始源代码没有您的操作系统所期望的那种换行符......
  • 使用 Fiddler 验证 WebClient 确实运行正常。然后,看看别处。
  • 我在 Windows 上使用 FileZilla 进行 FTP 传输。我在记事本中编写了测试文件,然后尝试将其作为二进制文件发送。这保留了换行符。然后我尝试了自动传输设置,当我再次下载它时,它缺少换行符。我猜 FileZilla 将新行更改为 Unix,然后 WebClient 保留了它,因此在记事本中查看它,该文件仍然具有 Unix 结尾

标签: c# .net webclient


【解决方案1】:

我认为您正在使用记事本查看下载的文件。尝试其他编辑器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 2012-04-13
    • 2011-01-01
    • 2011-02-06
    • 2014-10-30
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多