1

Could anyone please help me convert this code snippet to it's .net core equivalent?

        Uri uri = new Uri(url);
        string filename = System.IO.Path.GetFileName(uri.LocalPath);
        string extension = Path.GetExtension(filename);

        string tempFilepath = Path.GetTempFileName() + extension;

        try
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFile(url, tempFilepath);

            if (new FileInfo(tempFilepath).Length > 0)
            {
                return tempFilepath;
            }
            else {
                return null;
            }

        }
        catch (WebException e)
        {
            return null;
        }
        catch (NotSupportedException e)
        {
            return null;
        }

Actually, this code was previously in an application which was writte in .net 4.6. Then some time ago, we stopped using that app. Now I am developing another app in .net core and will be doing exact same thing. So I wonder how would I do this using .net core? What is the alternate to DownloadFile method in HttpClient?

Syed
  • 329
  • 2
  • 6
  • 18
  • 2
    Not sure why people are down voting this. @Syed did the diligence to write code out himself, and this question is about the changes in the .NET Core framework more than asking for a handout. – Ryan Mendoza May 26 '17 at 16:37

2 Answers2

3

This should do it...

try
{
    using (var client = new HttpClient())
    {
        using (HttpResponseMessage response = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).Result)
        {
            response.EnsureSuccessStatusCode();

            using (Stream contentStream = response.Content.ReadAsStreamAsync().Result, fileStream = new FileStream(tempFilepath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
            {
                var buffer = new byte[8192];
                var isMoreToRead = true;

                do
                {
                    var read = contentStream.ReadAsync(buffer, 0, buffer.Length).Result;
                    if (read == 0)
                    {
                        isMoreToRead = false;
                    }
                    else
                    {
                        fileStream.WriteAsync(buffer, 0, read);
                    }
                }

                while (isMoreToRead);
            }
        }
    }

Or you could implement this more cleanly like so: How to implement progress reporting for Portable HttpClient

Community
  • 1
  • 1
Ryan Mendoza
  • 920
  • 1
  • 13
  • 27
  • 1
    Thank you very much Ryan! This is exactly what I was looking for. Worked like a charm. Hats off!! – Syed Mar 27 '17 at 18:46
  • 1
    @Syed if your method is async, I would recommend awaiting the `ReadAsStreamAsync` and `ReadAsync` instead of doing `.Result`. – Ryan Mendoza Mar 27 '17 at 19:37
  • yes, I already changed it to that. But thanks for the hint :) – Syed Mar 29 '17 at 07:28
0

A more simpler way of doing it is ...

        HttpClient client = new HttpClient();
        try
        {
            var response =  client.GetStringAsync(url);
            Console.WriteLine(response.Result);
        }
        catch (Exception e)
        {
            Console.WriteLine("Message :{0} ", e.Message);
        }
        finally{
            client.Dispose();
        }

Target Framework : netcoreapp1.1
Editor : Visual Studio Code
Application Type : Console application

Kifayat Ullah
  • 579
  • 1
  • 5
  • 14