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?

1
  • 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. Commented May 26, 2017 at 16:37

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much Ryan! This is exactly what I was looking for. Worked like a charm. Hats off!!
@Syed if your method is async, I would recommend awaiting the ReadAsStreamAsync and ReadAsync instead of doing .Result.
yes, I already changed it to that. But thanks for the hint :)
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.