C# using WebClient to download chunked encoded content

 ̄綄美尐妖づ 提交于 2019-12-22 04:57:09

问题


I wrote a client application that is suppose to download a file from a web server, very simple:

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

The web site (where audio file located: http://localhost/audiotest/audio.wav) has header Transfer-Encoding: chunked

When I run the program, I get following error:

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

How can I download the file when server contains Transfer-Encoding: chunked header?


回答1:


I haven't tried it but this might work:

If you forcefully send an request for Http 1.0 rather than Http 1.1 then server will reply with HTTP Header specifying Content-Length

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

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

You will get the file as stream in response.GetResponseStream()

All credit to author of this



来源:https://stackoverflow.com/questions/10006669/c-sharp-using-webclient-to-download-chunked-encoded-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!