System.Net.Http.HttpRequestMessage.Dispose C# (CSharp) Method

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void
        protected virtual void Dispose(bool disposing)
        {
            // The reason for this type to implement IDisposable is that it contains instances of types that implement
            // IDisposable (content). 
            if (disposing && !_disposed)
            {
                _disposed = true;
                if (_content != null)
                {
                    _content.Dispose();
                }
            }
        }

Same methods

HttpRequestMessage::Dispose ( ) : void

Usage Example

        public IEnumerable<Hobbit> GetHobbits()
        {
            string reasonPhrase;

            using (var client = new HttpClient { BaseAddress = new Uri(BaseUri) })
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "/api/hobbit");
                request.Headers.Add("Accept", "application/json");

                var response = client.SendAsync(request);

                var content = response.Result.Content.ReadAsStringAsync().Result;
                var status = response.Result.StatusCode;

                reasonPhrase = response.Result.ReasonPhrase;

                request.Dispose();
                response.Dispose();

                if (status == HttpStatusCode.OK)
                {
                    return !string.IsNullOrEmpty(content) ?
                        JsonConvert.DeserializeObject<IEnumerable<Hobbit>>(content)
                        : null;
                }

                throw new Exception(reasonPhrase);
            }
        }
All Usage Examples Of System.Net.Http.HttpRequestMessage::Dispose