Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Windows.Web.Http在客户端设备上下载文件_C#_Rest_Xaml_Uwp_Windows 10 Universal - Fatal编程技术网

C# 如何使用Windows.Web.Http在客户端设备上下载文件

C# 如何使用Windows.Web.Http在客户端设备上下载文件,c#,rest,xaml,uwp,windows-10-universal,C#,Rest,Xaml,Uwp,Windows 10 Universal,我正在开发一个Windows到UWP应用程序。存在一个web服务,它在调用(GET)时返回一个文件。使用浏览器触发web服务时,它会在浏览器上成功下载文件 在UWP应用程序上,我使用Windows.Web.Http调用Web服务。我需要保存web服务发送的文件并将其保存到设备上 我目前有以下代码。不确定如何从web服务获取结果并保存到文件 public async Task DownloadFile(string WebServiceURL, string PathToSave) {

我正在开发一个Windows到UWP应用程序。存在一个web服务,它在调用(GET)时返回一个文件。使用浏览器触发web服务时,它会在浏览器上成功下载文件

在UWP应用程序上,我使用
Windows.Web.Http
调用Web服务。我需要保存web服务发送的文件并将其保存到设备上

我目前有以下代码。不确定如何从web服务获取结果并保存到文件

public async Task DownloadFile(string WebServiceURL, string PathToSave)
{

    var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
    myFilter.AllowUI = false;
    Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(myFilter);
    Windows.Web.Http.HttpResponseMessage result = await client.GetAsync(new Uri(WebServiceURL));


    using (IInputStream inputStream = await result.Content.ReadAsInputStreamAsync())
    {
        //not sure if this is correct and if it is, how to save this to a file
    }

}
使用
System.Web.Http
,我可以通过以下方式轻松完成此操作:

Stream stream = result.Content.ReadAsStreamAsync().Result;
var fileStream = File.Create(PathToSave);
await stream.CopyToAsync(fileStream);
fileStream.Dispose();
stream.Dispose();
但是,使用
Windows.Web.Http
,我不知道如何才能做到这一点。请帮忙

这就是你要找的? 像这样

var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
            myFilter.AllowUI = false;
            Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(myFilter);
            Windows.Web.Http.HttpResponseMessage result = await client.GetAsync(new Uri(WebServiceURL));

            //not sure if this is correct and if it is, how to save this to a file
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename.tmp", CreationCollisionOption.GenerateUniqueName);
            using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                await result.Content.WriteToStreamAsync(filestream);
                await filestream.FlushAsync();

            }
这就是你要找的? 像这样

var myFilter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
            myFilter.AllowUI = false;
            Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(myFilter);
            Windows.Web.Http.HttpResponseMessage result = await client.GetAsync(new Uri(WebServiceURL));

            //not sure if this is correct and if it is, how to save this to a file
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("filename.tmp", CreationCollisionOption.GenerateUniqueName);
            using (var filestream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                await result.Content.WriteToStreamAsync(filestream);
                await filestream.FlushAsync();

            }

啊,是的!谢谢!你能不能也检查一下你是否能在这里找到我的另一个问题?谢谢您应该避免
FlushAsync
-使用
块将关闭资源。啊,是的!谢谢!你能不能也检查一下你是否能在这里找到我的另一个问题?谢谢您应该避免使用
FlushAsync
-
using
块无论如何都会关闭资源。