1

I am trying to download a xlsx-attachment using a post-request but don't have a clue how to recieve the xlsx file. I found several different solutions which used a get-request which is not applicable in this case since i have to send data to the server which creates the file specificqally to the sent filters.

The following is the current situation:
1. I send a Post request with several Parameters
2. The server generates a xlsx file based on the parameters of the post request
3. I receive the data (but i dont know how to obtain the xlsx out of the data)

The data has to be somewhere as shown in the screenshot below but neigther in the response header, nor in the paramenters or the response content I can find data which could be a file.
enter image description here

This is the Header of the response:

Request URL:https://www.example.com/en/list.xlsx
Request Method:POST
Status Code:200 
Remote Address:104.25.95.108:443
Referrer Policy:no-referrer-when-downgrade

Response Headers
cache-control:max-age=0, private, no-store, no-cache, must-revalidate
cf-ray:38a32289090a26d8-FRA
content-disposition:attachment; filename="secondary.xlsx"
content-length:2017481
content-type:application/vnd.openxmlformats
officedocument.spreadsheetml.sheet; name=secondary.xlsx
date:Sun, 06 Aug 2017 15:47:40 GMT
expires:Sun, 06 Aug 2017 15:47:28 GMT
pragma:public
server:cloudflare-nginx
set-cookie:alive=1; expires=Sun, 06-Aug-2017 17:47:40 GMT; Max-Age=7200; path=/
status:200
strict-transport-security:max-age=63072000
x-content-type-options:nosniff
x-content-type-options:nosniff
x-frame-options:sameorigin
x-frame-options:DENY

this is my current code:

public static string POST(string extendURI, IEnumerable<KeyValuePair<string, string>> values)
    {
        var content = new FormUrlEncodedContent(values);
        var response = GlobalVar.client.PostAsync(GlobalVar.baseURI + extendURI, content).Result;
        string responseString = response.Content.ReadAsStringAsync().Result;
        return responseString;
    }

EDIT: Solution: get the system.io.stream and write to file

public static string Download_XML(string extendURI, IEnumerable<KeyValuePair<string, string>> values)
    {
        var content = new FormUrlEncodedContent(values);
        var response = GlobalVar.client.PostAsync(GlobalVar.mintosURI + extendURI, content).Result;

        string responseString = response.Content.ReadAsStreamAsync().Result.ToString();
        Stream stream = response.Content.ReadAsStreamAsync().Result;
        using (FileStream file = new FileStream("testfile.xlsx", FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, (int)stream.Length);
            file.Write(bytes, 0, bytes.Length);
            stream.Close();
        }
        //string responseString = response.Result.Content.ReadAsStringAsync().ToString();
        return responseString;
    }
Julian Bechtold
  • 167
  • 1
  • 12
  • I dont see any code where you are processing/creating any xlxs file. Can you please add some more detail ? – ManishKumar Aug 06 '17 at 15:57
  • Hello Manish, Thats the issue. I dont get any further in processing the xlsx file. I tried several things. Unless I don't have any Data to process I cannot create an xlsx file. the first step would be to get a datastream or another way to acess the downloaded data – Julian Bechtold Aug 06 '17 at 16:02
  • where do you want to process your data? and where you want to download it? I am not getting your question? – ManishKumar Aug 06 '17 at 16:07
  • I am trying to download the xlsx file from https://www.mintos.com/en/market/secondary/list.xlsx (dont know if posting links here is legal) For now I just want to download the file to my computer – Julian Bechtold Aug 06 '17 at 16:10
  • Normally you can search the market by parameters and then you can click on then a post-request ist sent with your filterparameters and the browser opens a window "open or safe file" – Julian Bechtold Aug 06 '17 at 16:13
  • this might help you to download file from external server https://stackoverflow.com/questions/8948364/how-to-initialize-file-download-from-an-external-server – ManishKumar Aug 06 '17 at 16:13
  • unfortunately not, since i have to send a set of parameters to the server. hence the post request rather than a simple client.download with parameters I mean the http content such as "max_term:24" – Julian Bechtold Aug 06 '17 at 16:18
  • I got it, but its totally depends on your server. If server is not accepting these parameters then you cant do anything about it? Are you the owner of file server? – ManishKumar Aug 06 '17 at 16:20
  • no im not owner of the server :D but the server accepts my parameters. I get response 200:OK + the memory jumps up exatly the size of the file when i execute my code. – Julian Bechtold Aug 06 '17 at 16:24
  • Ok, what's the type of response you are getting? what is the type of object? – ManishKumar Aug 06 '17 at 16:25
  • the response is of type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name=secondary-market.xlsx if i understand that right – Julian Bechtold Aug 06 '17 at 16:30
  • so this "responseString" contains your final data? – ManishKumar Aug 06 '17 at 16:34
  • it should but the string contains the following: PK??? - ??? beeing 3 unreadable squares – Julian Bechtold Aug 06 '17 at 16:35
  • what does "response" contains? is it a memory steam? – ManishKumar Aug 06 '17 at 16:37
  • Response Type: System.Net.Http.StreamContent – Julian Bechtold Aug 06 '17 at 16:53
  • Tried the following: string responseString = response.Content.ReadAsStreamAsync().Result.ToString(); resul: System.IO.MemoryStream – Julian Bechtold Aug 06 '17 at 16:55
  • NICE thank you. you helped me to get to the solution :) really nice ill update my question – Julian Bechtold Aug 06 '17 at 17:01
  • Ok. Did you get the solution? – ManishKumar Aug 06 '17 at 17:02

2 Answers2

0

try this

HttpResponseMessage finalResponse = Request.CreateResponse();

 finalResponse.Headers.AcceptRanges.Add("bytes");
    finalResponse.StatusCode = HttpStatusCode.OK;
    finalResponse.Content = new StreamContent(response);
    finalResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("render");
    finalResponse.Content.Headers.ContentDisposition.FileName = "fileName";
    finalResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("<set media type>");
    finalResponse.Content.Headers.ContentLength = response .Length;

return finalResponse;
ManishKumar
  • 1,476
  • 2
  • 14
  • 22
0

Dont forget to safe your code against network failures

public static void Download_File_Post(string extendURI, IEnumerable<KeyValuePair<string, string>> values)
{
    //create post parameters
    var content = new FormUrlEncodedContent(values);
    var response = GlobalVar.client.PostAsync(GlobalVar.baseURI + extendURI, content).Result;

    Stream stream = response.Content.ReadAsStreamAsync().Result;
        using (FileStream file = new FileStream("testfile.xlsx", FileMode.Create, System.IO.FileAccess.Write))
    {
        byte[] bytes = new byte[stream.Length];
        stream.Read(bytes, 0, (int)stream.Length);
        file.Write(bytes, 0, bytes.Length);
        stream.Close();
    }
}
Julian Bechtold
  • 167
  • 1
  • 12