1

I've written an UWP-App to download files via REST-Requests. The App runs on a Raspberry Pi 3 with Windows IoT.

I want to write 'testStream' into a .zip-File and download it. . Here is my code:

try
        {
            Uri uri = new Uri("http://xxxx:3080/cds/api/download/book/46795403-de-DE");
            HttpWebRequest getRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
            getRequest.Method = "GET";
            getRequest.Headers["Authorization"] = "Bearer xyxyxy";
            HttpWebResponse response2 = await getRequest.GetResponseAsync() as HttpWebResponse;
            StreamReader streamReader2 = new StreamReader(response2.GetResponseStream());
            Stream testStream = response2.GetResponseStream();
            getResult = streamReader2.ReadToEnd();
            textBox2.Text = getResult;

            await Task.Run(() =>
            {
                Task.Yield();
                using (var fs = File.Create("\\myZipDownload.zip"))
                {
                    testStream.CopyTo(fs);
                }
            });
        }

I'm getting this error:

"Access to the path 'C:\myZipDownload.zip' is denied."
k.troy2012
  • 344
  • 4
  • 21

1 Answers1

1

You need a writable destination.

You can use Environment.GetFolderPath to get a writable path.

how-to-get-temporary-folder-for-current-user

Linefinc
  • 375
  • 2
  • 9
  • Thanks! I can download the file now. But its size is 0 Bytes. Where am I failing? – k.troy2012 Aug 17 '17 at 13:23
  • 1
    @k.troy2012, not sure, I know that `GetResponse`/`GetResponseAsync` will return the same `HttpWebResponse` object, not a "new" one, so perhaps because you're making two calls to `GetResponseStream` then the second time the stream has already been read and would require seeking to the start to read again (which I'm not sure is possible for web responses...). Try removing all calls to `GetResponseStream` except for the one you'll use to write to your file. Also remember to call `Dispose` on this, or wrap it in a `using` statement. – easuter Aug 17 '17 at 13:46
  • I removed the other call tho GetResponseStream and now it works fine! – k.troy2012 Aug 17 '17 at 14:00