Let's suppose having a web site, from which i can download .xml files compressed in .zip archives, and i want to write a code snippet that executes the download of these archives. I precise that this web site doesn't provide any APIs (RESTful or not), so i tried to emulate the behavior of a user that physically clicks on the download button. I'm using RestSharp and the first part of the request is something like this:
RestRequest postDownloadRequest = new RestRequest(Settings.DownloadUrl, Method.Post);
postDownloadRequest.AddParameter("application/x-www-form-urlencoded", downloadBodyContent.ReadAsStringAsync().Result, ParameterType.RequestBody);
Task<RestResponse> postDownloadResponseTask = client.ExecuteAsync(postDownloadRequest);
postDownloadResponseTask.Wait();
This code is correct and the request returns 200.
Since postDownloadResponseTask.Result.Content
contains a long string and the Transfer-Encoding
header of the response contains the value chanked
, i suppose that this is the bytes array of the chunked downloaded archive, converted to string. Is this correct?
Now i want to rebuild the initial zip archive and to save it in my local machine. I tried to convert the string in byte[] and than in a stream, and to build a ZipArchive by passing the stream, but it didn't work. How can i do this as correct as possible?
You can do it by:
byte[] zipBytes = postDownloadResponseTask.Result.RawBytes;
string path = @"C:\path\your-file-name.zip";
File.WriteAllBytes(path, zipBytes);