gpt4 book ai didi

C# 使用 WebClient 下载分块编码内容

转载 作者:太空狗 更新时间:2023-10-29 20:31:31 27 4
gpt4 key购买 nike

我写了一个客户端应用程序,它假设从网络服务器下载一个文件,非常简单:

using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://localhost/audiotest/audio.wav",
@"C:\audio.wav");
}

该网站(音频文件所在的位置:http://localhost/audiotest/audio.wav)具有 header Transfer-Encoding: chunked

当我运行程序时,出现以下错误:

The server committed a protocol violation. Section=ResponseBody Detail=Response chunk format is invalid

当服务器包含 Transfer-Encoding: chunked header 时,如何下载文件?

最佳答案

我还没有尝试过,但这可能有效:

如果您强制发送 Http 1.0 而不是 Http 1.1 的请求,那么服务器将回复 指定内容长度的 HTTP header

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://localhost/audiotest/audio.wav");
wr.ProtocolVersion = Version.Parse("1.0");

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

您将在 response.GetResponseStream() 中以流的形式获取文件

全部归功于 this 的作者

关于C# 使用 WebClient 下载分块编码内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10006669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com