Beispiel #1
0
        public virtual async Task <HTTPResult> ExecuteGet(string requestUri, bool cacheControlHeadersEnabled = false)
        {
            var result = new HTTPResult();

            var request = new HttpRequestMessage
            {
                RequestUri = new Uri(_httpClient.BaseAddress, requestUri),
                Method     = HttpMethod.Get
            };

            if (cacheControlHeadersEnabled)
            {
                request.Headers.Add(Constants.Http.CacheControlKey, Constants.Http.CacheControlValue);
            }

            try
            {
                using (var response = await _httpClient.SendAsync(request))
                {
                    result.statusCode = response.StatusCode;
                    result.content    = await response.Content.ReadAsStringAsync();
                }
            }
            catch (Exception e)
            {
                _log.Error(string.Format("Exception caught executing GET {0}", requestUri), e);
            }

            return(result);
        }
Beispiel #2
0
        public virtual HTTPResult ExecutePost(string requestUri, string data)
        {
            var result = new HTTPResult();

            try
            {
                var task = httpClient.PostAsync(requestUri, new StringContent(data, Encoding.UTF8, "application/json"));
                task.Wait();
                var response = task.Result;
                result.statusCode = response.StatusCode;
                result.content    = response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Exception caught executing POST {0}", requestUri), e);
            }
            return(result);
        }
Beispiel #3
0
        public virtual async Task <HTTPResult> ExecutePost(string requestUri, string data, CancellationToken token = default(CancellationToken))
        {
            var result = new HTTPResult();

            try
            {
                using (var response = await httpClient.PostAsync(requestUri, new StringContent(data, Encoding.UTF8, "application/json"), token))
                {
                    result.statusCode = response.StatusCode;
                    result.content    = response.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Exception caught executing POST {0}", requestUri), e);
            }
            return(result);
        }
Beispiel #4
0
        public virtual HTTPResult ExecuteGet(string requestUri)
        {
            var result = new HTTPResult();

            try
            {
                var task = httpClient.GetAsync(requestUri);
                task.Wait();
                var response = task.Result;
                result.statusCode = response.StatusCode;
                result.content    = response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Exception caught executing GET {0}", requestUri), e);
            }
            return(result);
        }
Beispiel #5
0
        public virtual async Task <HTTPResult> ExecuteGet(string requestUri, CancellationToken token = default(CancellationToken))
        {
            var result = new HTTPResult();

            try
            {
                using (var response = await httpClient.GetAsync(requestUri, token))
                {
                    result.statusCode = response.StatusCode;
                    result.content    = response.Content.ReadAsStringAsync().Result;
                }
            }
            catch (Exception e)
            {
                Log.Error(String.Format("Exception caught executing GET {0}", requestUri), e);
            }
            return(result);
        }