C# 从windows/console应用程序中的WEB API响应获取流数据

C# 从windows/console应用程序中的WEB API响应获取流数据,c#,asp.net-web-api,download,stream,C#,Asp.net Web Api,Download,Stream,我需要通过流数据将文件从WEB API下载到windows应用程序 WEB API: public HttpResponseMessage download(string fileid){ var response = new HttpResponseMessage (); response.statuscode= HttpStatusCode.Ok; response.Content =new StreamContent(filestream);// I have a filestr

我需要通过流数据将文件从WEB API下载到windows应用程序

WEB API:

public HttpResponseMessage download(string fileid){

var response = new HttpResponseMessage ();    
response.statuscode= HttpStatusCode.Ok;
response.Content =new StreamContent(filestream);// I have a filestream here .
return response;
此处显示添加内容后添加的内容配置和内容类型。 在客户端,我尝试了以下方法

HttpResponseMessage file = httpclinet.GetAsync("url").Result();

Var stream = file.Content.ReadAsStreamAsync().Result();

using (var data = File.create(@"somepath.txt"))
{
    data.seek(0,seekorigin.begin);

    stream.copyto(data);

}
但是我没有得到输出。我得到的是 流详细信息,如
Stream.content
statuscode
的版本对象。就像那个文件是写的一样。
如何在此文件中写入流数据。

我建议您使用此处的关键字,您可以这样更改代码以获得结果:

HttpResponseMessage file = await httpclinet.GetAsync("url");
var stream = await file.Content.ReadAsStreamAsync()
您还需要向方法中添加
async
关键字,如下所示:

public async Task<HttpResponseMessage> download(string fileid)
公共异步任务下载(字符串fileid)

我尝试了你所说的。收到了相同的答案。我认为没有太大区别。我的方法可以吗???@user9949432您也需要在这里添加它:
var stream=await file.Content.ReadAsStreamAsync()
是的,我在两个地方都添加了await,但文件写错了。