在 C# 中使用 Post 下载附件文件

Download Attachment file using Post in c#

我正在尝试使用 post-request 下载 xlsx-attachment,但不知道如何接收 xlsx 文件。我发现了几种不同的解决方案,它们使用了 get-request,但在这种情况下不适用,因为我必须将数据发送到服务器,服务器专门为发送的过滤器创建文件。

目前情况如下:
1. 我发送了一个 Post 请求,其中包含几个参数
2、服务端根据post请求的参数生成一个xlsx文件
3. 我收到了数据(但我不知道如何从数据中获取xlsx)

数据必须位于以下屏幕截图所示的某个位置,但不在响应 header 中,也不在参数或响应内容中,我可以找到可能是文件的数据。

这是响应的Header:

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

这是我当前的代码:

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

编辑: 解决方案:获取 system.io.stream 并写入文件

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

试试这个

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;

不要忘记保护您的代码免受网络故障的影响

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