C # using WebClient to load encoded content

I wrote a client application that supposedly downloads a file from a web server, very simple:

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

The website (where the audio file is located: http: //localhost/audiotest/audio.wav ) has the title Transfer-Encoding: chunked

When I run the program, I get the following error:

The server committed a protocol violation. Section = ResponseBody Detail = The format of the response fragment is invalid

How to download a file when the server contains Transfer-Encoding: chunked header?

+7
source share
2 answers

I have not tried, but this may work:

If you force a request for Http 1.0 instead of Http 1.1, then the server will respond with an 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 receive the file as a stream in response.GetResponseStream()

All words of the author this

+4
source

All Articles