提问人:Nigel Tunnicliffe 提问时间:11/9/2023 更新时间:11/9/2023 访问量:16
无法使用 C# Web 客户端从 Web 服务器下载 .lst 文件
Cannot download .lst files from web server using C# Web Client
问:
我正在使用 C# WebClient 从 Web 服务器下载文件。我的代码是这样的
using (var Client = new WebClient()
{
Client.DownloadFile(Source, Target);
}
如果要下载的文件的文件扩展名是 .xml 或 .txt,这一切都可以正常工作,但如果是 .lst,即使我知道文件存在,我也会收到 404 错误说“未找到”。我尝试将扩展添加到IIS中允许的扩展列表中,但仍然收到相同的错误。.lst 文件在资源管理器中显示为“MASM 列表”文件 - 这些文件是否被阻止下载?
我还尝试使用HTTP客户端下载文件,但结果相同。除了在下载后更改文件名之外,是否有任何方法可以允许下载 .lst 文件。
谢谢
答: 暂无答案
评论
using (var client = new HttpClient()) { // Add headers if needed client.DefaultRequestHeaders.Add("User-Agent", "Your User Agent String"); var response = await client.GetAsync("url to .lst file"); if (response.IsSuccessStatusCode) { var fileBytes = await response.Content.ReadAsByteArrayAsync(); File.WriteAllBytes("path to save the file", fileBytes); } else { // Handle error response } }