DownloadFileAsync 下载损坏的文件

DownloadFileAsync downloading corrupted file

我正在尝试构建下载 class。来自 GitHub 的文件下载正常,但是如果我下载 ROBLOX 资产(例如 http://www.roblox.com/asset/?id=1286103),它会下载一个充满垃圾数据的文件,而它应该下载以 [=] 开头的 ROBLOX 网格文件17=] "version 1.00".

    public void InitDownload(string additionalText = "")
    {
        downloadOutcomeAddText = additionalText;

        saveFileDialog1 = new SaveFileDialog()
        {
            FileName = fileName,
            //"Compressed zip files (*.zip)|*.zip|All files (*.*)|*.*"
            Filter = fileFilter,
            Title = "Save " + fileName
        };

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                    wc.DownloadFileAsync(new Uri(fileURL), saveFileDialog1.FileName);
                }

                downloadOutcome = "File " + Path.GetFileName(saveFileDialog1.FileName) + " downloaded!" + downloadOutcomeAddText;
            }
            catch (Exception ex)
            {
                downloadOutcome = "Error when downloading file: " + ex.Message;
            }
        }
    }

    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        downloadProgress.Value = e.ProgressPercentage;
    }

看起来 ROBLOX API 正在使用 gzip 编码数据进行响应,显然 WebClient.DownloadFileAsync 不会默认自动解压缩响应主体。有关如何处理此问题的更多信息和有点过时的示例,请参阅:https://blog.codinghorror.com/netwebclient-and-gzip/.

更新: 这个 Stack Overflow 答案可能也有帮助(它更简单并且不需要第 3 方库):Uncompressing gzip response from WebClient