【问题标题】:C# using WebClient to download chunked encoded contentC# 使用 WebClient 下载分块编码内容
【发布时间】:2012-04-17 21:33:40
【问题描述】:

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

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

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

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

服务器违反了协议。部分=响应正文 Detail=响应块格式无效

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

【问题讨论】:

    标签: c# chunked-encoding


    【解决方案1】:

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

    如果您强制发送 Http 1.0 而不是 Http 1.1 的请求,那么服务器将回复 HTTP Header 指定 Content-Length

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

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

    感谢this的作者

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 2020-11-29
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多