HttpClient ObjectDisposedException Android [英] HttpClient ObjectDisposedException Android

查看:72
本文介绍了HttpClient ObjectDisposedException Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试服务器停机时对我的API的HTTP请求. 它应该收到一个错误响应,但是,它返回null,并给了我这个异常: System.ObjectDisposedException:无法访问关闭的Stream.

I am testing an HTTP Request to my API while the server is down. It should receive an error response, but instead, it returns null and it gives me this exception: System.ObjectDisposedException: Cannot access a closed Stream.

这仅在Android上发生,我得到一个错误响应.这是我的代码:

This happens in Android only, iOS I get an error response. this is my code:

using (HttpClient client = new HttpClient())
{
    try
    {
        //pedido de token

        var loginInfo = new StringContent(JsonConvert.SerializeObject(userAuth).ToString(), Encoding.UTF8, "application/json");

        var requestToken = await client.PostAsync(URLs.url + URLs.getToken, loginInfo);
        var receiveToken = await requestToken.Content.ReadAsStringAsync();

它没有到达ReadAsString,在PostAsync中引发异常.

It doesn't reach the ReadAsString, throws the exception in the PostAsync.

推荐答案

请勿丢弃HttpClient.它被设计为可重用并处理多个同时请求.

Don't dispose of HttpClient. It is designed to be reused and handle multiple simultaneous requests.

此处提供有关HttpClient工作方式的更多信息: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

Here's more info about how HttpClient works: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

这是我在Xamarin.Forms应用程序中用于所有HttpClient服务的通用实现:

Here is a generic implementation that I use for all HttpClient services in my Xamarin.Forms apps:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Http;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;

using Newtonsoft.Json;

using Xamarin.Forms;

namespace NameSpace
{
    public abstract class BaseHttpClientService
    {
        #region Constant Fields
        static readonly Lazy<JsonSerializer> _serializerHolder = new Lazy<JsonSerializer>();
        static readonly Lazy<HttpClient> _clientHolder = new Lazy<HttpClient>(() => CreateHttpClient(TimeSpan.FromSeconds(30)));
        #endregion

        #region Fields
        static int _networkIndicatorCount = 0;
        #endregion

        #region Events
        public static event EventHandler<string> HttpRequestFailed;
        #endregion

        #region Properties
        static HttpClient Client => _clientHolder.Value;
        static JsonSerializer Serializer => _serializerHolder.Value;
        #endregion

        #region Methods
        protected static async Task<T> GetObjectFromAPI<T>(string apiUrl)
        {
            using (var responseMessage = await GetObjectFromAPI(apiUrl).ConfigureAwait(false))
                return await DeserializeResponse<T>(responseMessage).ConfigureAwait(false);
        }

        protected static async Task<HttpResponseMessage> GetObjectFromAPI(string apiUrl)
        {
            try
            {
                UpdateActivityIndicatorStatus(true);

                return await Client.GetAsync(apiUrl).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                OnHttpRequestFailed(e.Message);
                Report(e);
                throw;
            }
            finally
            {
                UpdateActivityIndicatorStatus(false);
            }
        }

        protected static async Task<TResponse> PostObjectToAPI<TResponse, TRequest>(string apiUrl, TRequest requestData)
        {
            using (var responseMessage = await PostObjectToAPI(apiUrl, requestData).ConfigureAwait(false))
                return await DeserializeResponse<TResponse>(responseMessage).ConfigureAwait(false);
        }

        protected static Task<HttpResponseMessage> PostObjectToAPI<T>(string apiUrl, T requestData) => SendAsync(HttpMethod.Post, apiUrl, requestData);

        protected static async Task<TResponse> PutObjectToAPI<TResponse, TRequest>(string apiUrl, TRequest requestData)
        {
            using (var responseMessage = await PutObjectToAPI(apiUrl, requestData).ConfigureAwait(false))
                return await DeserializeResponse<TResponse>(responseMessage).ConfigureAwait(false);
        }

        protected static Task<HttpResponseMessage> PutObjectToAPI<T>(string apiUrl, T requestData) => SendAsync(HttpMethod.Put, apiUrl, requestData);

        protected static async Task<TResponse> PatchObjectToAPI<TResponse, TRequest>(string apiUrl, TRequest requestData)
        {
            using (var responseMessage = await PatchObjectToAPI(apiUrl, requestData).ConfigureAwait(false))
                return await DeserializeResponse<TResponse>(responseMessage).ConfigureAwait(false);
        }

        protected static Task<HttpResponseMessage> PatchObjectToAPI<T>(string apiUrl, T requestData) => SendAsync(new HttpMethod("PATCH"), apiUrl, requestData);

        protected static async Task<TResponse> DeleteObjectFromAPI<TResponse>(string apiUrl)
        {
            using (var responseMessage = await DeleteObjectFromAPI(apiUrl).ConfigureAwait(false))
                return await DeserializeResponse<TResponse>(responseMessage).ConfigureAwait(false);
        }

        protected static Task<HttpResponseMessage> DeleteObjectFromAPI(string apiUrl) => SendAsync<object>(HttpMethod.Delete, apiUrl);

        static HttpClient CreateHttpClient(TimeSpan timeout)
        {
            HttpClient client;
            switch (Device.RuntimePlatform)
            {
                case Device.iOS:
                case Device.Android:
                    client = new HttpClient();
                    break;
                default:
                    client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip });
                    break;
            }

            client.Timeout = timeout;
            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));

            return client;
        }

        static async Task<HttpResponseMessage> SendAsync<T>(HttpMethod httpMethod, string apiUrl, T requestData = default)
        {
            using (var httpRequestMessage = await GetHttpRequestMessage(httpMethod, apiUrl, requestData).ConfigureAwait(false))
            {
                try
                {
                    UpdateActivityIndicatorStatus(true);

                    return await Client.SendAsync(httpRequestMessage).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    OnHttpRequestFailed(e.Message);
                    Report(e);
                    throw;
                }
                finally
                {
                    UpdateActivityIndicatorStatus(false);
                }
            }
        }

        protected static void UpdateActivityIndicatorStatus(bool isActivityIndicatorDisplayed)
        {
            if (isActivityIndicatorDisplayed)
            {
                Device.BeginInvokeOnMainThread(() => Application.Current.MainPage.IsBusy = true);
                _networkIndicatorCount++;
            }
            else if (--_networkIndicatorCount <= 0)
            {
                Device.BeginInvokeOnMainThread(() => Application.Current.MainPage.IsBusy = false);
                _networkIndicatorCount = 0;
            }
        }

        static async ValueTask<HttpRequestMessage> GetHttpRequestMessage<T>(HttpMethod method, string apiUrl, T requestData = default)
        {
            var httpRequestMessage = new HttpRequestMessage(method, apiUrl);

            switch (requestData)
            {
                case T data when data.Equals(default(T)):
                    break;

                case Stream stream:
                    httpRequestMessage.Content = new StreamContent(stream);
                    httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    break;

                default:
                    var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(requestData)).ConfigureAwait(false);
                    httpRequestMessage.Content = new StringContent(stringPayload, Encoding.UTF8, "application/json");
                    break;
            }

            return httpRequestMessage;
        }

        static async Task<T> DeserializeResponse<T>(HttpResponseMessage httpResponseMessage)
        {
            httpResponseMessage.EnsureSuccessStatusCode();

            try
            {
                using (var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false))
                using (var reader = new StreamReader(contentStream))
                using (var json = new JsonTextReader(reader))
                {
                    if (json is null)
                        return default;

                    return await Task.Run(() => Serializer.Deserialize<T>(json)).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                Report(e);
                throw;
            }
        }

        static void OnHttpRequestFailed(string message) => HttpRequestFailed?.Invoke(null, message);

        static void Report(Exception e, [CallerMemberName]string callerMemberName = "") => Debug.WriteLine(e.Message);
        #endregion
    }
}

这篇关于HttpClient ObjectDisposedException Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆