Julian Bechtold Julian Bechtold - 9 months ago 58
HTTP Question

Download Attachment file using Post in c#

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;
}

Answer Source

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();
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download