Ejemplo n.º 1
0
        private async Task <String> DownloadData(String uri)
        {
            var response = String.Empty;
            HttpResponseMessage httpResponseMessage;
            HttpClient          httpClient = new HttpClient();

            if (!NetworkHelper.CheckConnectivity())
            {
                throw new NetworkException("NO_NET");
            }

            try
            {
                httpResponseMessage = await httpClient.GetAsync(new Uri(uri));

                response = await httpResponseMessage.Content.ReadAsStringAsync();
            }
            catch (ArgumentNullException)
            {
                throw new ArgumentNullException("BAD_GET_URI");
            }

            switch (httpResponseMessage.StatusCode)
            {
            case System.Net.HttpStatusCode.OK:
            {
                // 200
                Debug.WriteLine("[ApiService.DownloadData]\t" + "HttpStatusCode 200 - OK");
                break;
            }

            case System.Net.HttpStatusCode.BadRequest:
            {
                // 400
                Debug.WriteLine("[ApiService.DownloadData]\t" + "HttpStatusCode 400 - BadRequest");
                throw new ApiException(response);
            }

            case System.Net.HttpStatusCode.Unauthorized:
            {
                // 401
                Debug.WriteLine("[ApiService.DownloadData]\t" + "HttpStatusCode 401 - Unauthorized");
                throw new ApiException(response);
            }

            case System.Net.HttpStatusCode.NotFound:
            {
                // 404
                Debug.WriteLine("[ApiService.DownloadData]\t" + "HttpStatusCode 404 - NotFound");
                throw new ApiException(response);
            }

            case System.Net.HttpStatusCode.InternalServerError:
            {
                // 500
                Debug.WriteLine("[ApiService.DownloadData]\t" + "HttpStatusCode 500 - InternalServerError");
                throw new ApiException(response);
            }
            }

            return(response);
        }