RestSharp.Portable从Stream读取文件

时间:2017-03-02 16:08:50

标签: c# rest portable-class-library restsharp

我们在项目中使用RestSarp.Portable。对于我们的大多数请求,这都是完美的。 但它对于通过流发送文件的get请求不起作用。我通过浏览器测试了请求手册。这很有效。

似乎RestSharp.Portable没有提供方法client.DownloadData(request) 所以知道我必须从响应中读出它。 我的代码是(此刻,不工作!)

var request = new RestRequest("someRequest", Method.GET);
var result = await client.Execute<HttpResponseMessage>(request);
var responseMessage = result.Data
var resultStream =  await responseMessage.Content.ReadAsStreamAsync();

result.Data接缝为null。现在我错过了什么?我的错在哪里

(这是我在服务器上的api方法:

 [Route("someRequest")]
 [HttpGet]
 public HttpResponseMessage getFile(){
 string path = getPath(req);
            try
            {
                MemoryStream responseStream = new MemoryStream();
                Stream fileStream = File.Open(path, FileMode.Open);
                fileStream.CopyTo(responseStream);
                fileStream.Close();
                HttpResponseMessage response = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StreamContent(responseStream)
                };
                response.Content.Headers.ContentDisposition =
                new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                {
                    FileName = Path.GetFileName(path)
                };
                response.Content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/octet-stream");
                return response;
            }
            catch (IOException)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }

0 个答案:

没有答案