C# HttpClient returns error 500 (Internal Server Error) once per several calls
<p>My web application connects to back-end using HttpClient. Generally it works, but once per 10=15 calls error 500 appears. When page is reloaded it goes ok. The error appears in code where connection is made to backend with HttpClient object. My web application is using <strong>Unity as IOC</strong>.</p>

<p>I made a few trials before current code:
- HttpClientHelper had static field  <code>static HttpClient httpClient</code> which was not disposed in code</p>

<p>but still error 500 appears.</p>

<p><strong>BUT</strong>: when I do a lot of calls from external app like Fiddler, the error is not appearing . I did about 150 quick calls and no error appeared.</p>

<p>UPDATE1:</p>

<p>My request from fiddler is :</p>

<pre><code>GET http://www.zzz.com/methodA HTTP/1.1
Authorization: Basic **********************
Accept: application/json
Accept-Language: en-US, en; q=0.5
Host:www.zzz.com
</code></pre>

<p>Response from fiddler is :</p>

<pre><code>HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 06 Sep 2016 14:06:34 GMT
Content-Length: 36

{"Message":"An error has occurred."}
</code></pre>

<p>UPDATE 2:</p>

<p>I changed code so httpClient is a static property. But still the same issue. <strong>The code of HttpClientHelper class is started from async/await, maybe this is a problem?</strong></p>

<p>UPDATE3:
I created new post, it's more detailed:
<a href="https://stackoverflow.com/questions/39367225/how-to-syncronize-static-valiable-inside-async-await-methods-httpclient-error-5">https://stackoverflow.com/questions/39367225/how-to-syncronize-static-valiable-inside-async-await-methods-httpclient-error-5</a>
You are welcome to cotinue;)</p>

<p>My current connection class is:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MicrositeEngine.Common.Helpers
{
    /// &lt;summary&gt;
    /// Helper class for http calls
    /// &lt;/summary&gt;
    public class HttpClientHelper
    {
        private IDictionary&lt;string, string&gt; _customHeaders { get; set; }

        public HttpClientHelper() { }

        public HttpClientHelper(IDictionary&lt;string, string&gt; customHeader)
        {
            _customHeaders = customHeader;
        }

        /// &lt;summary&gt;
        /// Asychronously download string data from given url
        /// &lt;/summary&gt;
        /// &lt;param name="url"&gt;Place for download string from&lt;/param&gt;
        /// &lt;returns&gt;String data downloaded from url&lt;/returns&gt;
        public async Task&lt;string&gt; GetStringAsync(string url)
        {
            HttpResponseMessage response = null;

            using (HttpClient httpClient = CreateHttpClient())
            {
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpRequestConsts.ContentTypeJson));
                httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
                httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");

                response = await httpClient.GetAsync(url).ConfigureAwait(true);
                response.EnsureSuccessStatusCode();
            }

            return await response.Content.ReadAsStringAsync().ConfigureAwait(true);
        }

        /// &lt;summary&gt;
        /// Asychronously download byte array data from given url
        /// &lt;/summary&gt;
        /// &lt;param name="url"&gt;Place for download byte array from&lt;/param&gt;
        /// &lt;returns&gt;Byte array data downloaded from url&lt;/returns&gt;
        public async Task&lt;byte[]&gt; GetByteArrayAsync(string url)
        {
            HttpResponseMessage response = null;

            using (HttpClient httpClient = CreateHttpClient())
            {
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HttpRequestConsts.ContentTypeJson));
                httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
                httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");

                response = await httpClient.GetAsync(url).ConfigureAwait(true);
                response.EnsureSuccessStatusCode();
            }

            return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
        }


        private HttpClient CreateHttpClient()
        {
            var clientHandler = new HttpClientHandler { };
            var client = new HttpClient(clientHandler);

            if (_customHeaders != null)
            {
                foreach (var item in _customHeaders)
                {
                    client.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }
            client.MaxResponseContentBufferSize = 2147483647; //max possible value

            return client;
        }
    }
}
</code></pre>