Example #1
0
        /// <summary>
        /// Executes the HttpWebRequest and return if the request successded or failed.
        /// Also returns the value from the request
        /// </summary>
        /// <typeparam name="T">Type of the return value</typeparam>
        /// <param name="http">Any instance of HttpWebRequest</param>
        /// <returns>Instance of Custom Reponse</returns>
        public static async Task <CustomResponse <T> > GetResponseOrExceptionAsync <T>(this HttpWebRequest http)
        {
            try
            {
                using (var response = await http.GetResponseAsync())
                {
                    using (var data = response.GetResponseStream())
                    {
                        using (var stream = new StreamReader(data))
                        {
                            var json = stream.ReadToEnd();
                            var o    = JsonConvert.DeserializeObject <T>(json);

                            return(CustomResponse <T> .Success(o));
                        }
                    }
                }
            }
            catch (WebException ex) when(ex.Response != null)
            {
                return(CustomResponse <T> .Error(ex.GetErrorMessage()));
            }
            catch (Exception ex)
            {
                return(CustomResponse <T> .Error(ex.Message));
            }
        }
Example #2
0
        /// <summary>
        /// Executes the HttpWebRequest and return if the request successded or failed.
        /// </summary>
        /// <param name="http">Any instance of HttpWebRequest</param>
        /// <returns>Instance of Custom Reponse</returns>
        public static async Task <CustomResponse> GetResponseOrExceptionAsync(this HttpWebRequest http)
        {
            try
            {
                await http.GetResponseAsync();

                return(CustomResponse.Success());
            }
            catch (WebException ex) when(ex.Response != null)
            {
                return(CustomResponse.Error(ex.GetErrorMessage()));
            }
            catch (Exception ex)
            {
                return(CustomResponse.Error(ex.Message));
            }
        }