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

时间:2017-08-06 15:52:02

标签: c# http post download attachment

我正在尝试使用post-request下载xlsx-attachment但是没有线索如何接收xlsx文件。我找到了几个使用get-request的不同解决方案,在这种情况下不适用,因为我必须将数据发送到服务器,该服务器创建特定于已发送过滤器的文件。

以下是目前的情况:
1.我发送一个带有多个参数的发布请求 2.服务器根据发布请求的参数生成xlsx文件 3.我收到数据(但我不知道如何从数据中获取xlsx)

数据必须在下面的屏幕截图中显示,但在响应标题中,在参数或响应内容中我都可以找到可能是文件的数据。 enter image description here

这是回复的标题:

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

2 个答案:

答案 0 :(得分:0)

试试这个

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;

答案 1 :(得分:0)

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

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