Exemplo n.º 1
0
        public ContentResponse PostAsJson <T>(T input, string url, params object[] args)
        {
            var escapedUrl = NormaliseUrl(url, args);
            var task       = Client.PostAsJsonAsync(escapedUrl, input);

            return(task.ContinueWith(x =>
            {
                if (x.IsFaulted)
                {
                    var response = new ContentResponse {
                        Faulted = true
                    };
                    if (x.Exception != null)
                    {
                        response.Message = x.Exception.Message;
                    }

                    return response;
                }

                var result = task.Result;

                if (!result.IsSuccessStatusCode)
                {
                    return CreateResponseIfNotSuccessful <ContentResponse>(x);
                }

                if (ResponseIsHtmlContentType(result))
                {
                    return new ContentResponse
                    {
                        Faulted = true,
                        Message = "Server responded with text/html (indicates a non-webapi/mvc data action has an unhandled exception)"
                    };
                }

                return new ContentResponse
                {
                    Faulted = false,
                    StatusCode = result.StatusCode,
                    Message = x.Result.ReasonPhrase
                };
            })
                   .Result);
        }
        public ContentResponse Get(string url, params object[] args)
        {
            var escapedUrl = NormaliseUrl(url, args);
            var task       = Client.GetAsync(escapedUrl);

            return(task.ContinueWith(httpTask =>
            {
                if (httpTask.IsFaulted)
                {
                    var response = new ContentResponse {
                        Faulted = true
                    };
                    if (httpTask.Exception != null)
                    {
                        response.Message = httpTask.Exception.Message;
                    }

                    return response;
                }

                var result = httpTask.Result;

                if (!result.IsSuccessStatusCode)
                {
                    return CreateResponseIfNotSuccessful <ContentResponse>(httpTask);
                }

                return new ContentResponse
                {
                    Faulted = false,
                    StatusCode = result.StatusCode,
                    Message = httpTask.Result.ReasonPhrase,
                    Content = result.Content.ReadAsStringAsync().Result
                };
            }).Result);
        }