Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T">T is sending content</typeparam>
        /// <typeparam name="R">R is receiving content</typeparam>
        /// <param name="uri"></param>
        /// <param name="method"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task <R> GetResponseObject <T, R>(string uri, eHttpMethodType method, T content)
        {
            R responseObject = default(R);

            var request = CreateWebRequest(string.Format("{0}{1}", _baseuri, uri), method, content);

            await HandleResponse(request, responseAsString =>
            {
                responseObject = JsonConvert.DeserializeObject <R>(responseAsString);
            });

            return(responseObject);
        }
Ejemplo n.º 2
0
        private HttpResponseMessage CreateWebRequest <T>(string uri, eHttpMethodType method, T content)
        {
            HttpResponseMessage response = null;

            using (var client = new HttpClient())
            {
                string jsonstring = null;
                if (!(content is bool) || content != null)
                {
                    jsonstring = JsonConvert.SerializeObject(content);
                }
                switch (method)
                {
                case eHttpMethodType.GET:
                    response = client.GetAsync(uri).Result;
                    break;

                case eHttpMethodType.POST:
                    if (jsonstring != null)
                    {
                        response = client.PostAsync(uri, new StringContent(jsonstring, Encoding.UTF8, "application/json")).Result;
                    }
                    break;

                case eHttpMethodType.PUT:
                    if (jsonstring != null)
                    {
                        response = client.PutAsync(uri, new StringContent(jsonstring, Encoding.UTF8, "application/json")).Result;
                    }
                    break;

                case eHttpMethodType.DELETE:
                    response = client.DeleteAsync(uri).Result;
                    break;
                }

                return(response);
            }
        }