我正在尝试构建一个下载类。从GitHub下载文件很好,但如果我下载ROBLOX资产(http://www.roblox.com/asset/?id=1286103例如(,它下载一个充满垃圾数据的文件,而它应该下载一个以标题"版本1.00"开头的ROBLOX网格文件
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/.
更新:这个堆栈溢出答案可能也很有用(它更简单,不需要第三方库(:从WebClient 中解压缩gzip响应