Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Using HttpClient

$
0
0

I'd like to use a HttpClient in a shared C# class. I've seen the HttpClient in a few Xamarin examples like this one: http://docs.xamarin.com/guides/cross-platform/advanced/async_support_overview

But somehow I cannot use "using System.Net.Http;" and I guess therefore HttpClient won't work as it's an unknown object.

Any ideas?

My code:

using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

using MyApp.Shared;

namespace MyApp.Shared
{
    public class FileDownloader
    {
        public byte[] GetFile {get; private set;}

        public async Task<bool> DownloadFile(string uri)
        {
            try
            {
                var httpClient = new HttpClient(); // Error CS0246: The type or namespace name `HttpClient' could not be found. Are you missing an assembly reference? (CS0246)
                this.GetFile = await httpClient.GetByteArrayAsync(uri);
            }
            catch (OperationCanceledException)
            {
                Debug.LogWarning("File download got canceled.", "FileDownloader");
                return false;
            }

            return true;
        }
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>