title: HttpClient Return error code 500 Server Internal Error
body: <p>This is the method Making the call:</p>
  
  <pre><code>public async System.Threading.Tasks.Task&lt;Session&gt; GetSessionFromLoginAsync()
   {
     try
        {
           var client = CustomHttpClient.GetClient();
           var httpRequest = PrepareRequest(_host, HttpMethod.Post, _connParameters);
              try
                  {
                    var httpResponse = await client.SendAsync(httpRequest);
                       if (!httpResponse.IsSuccessStatusCode)
                        {
                          throw CustomException.CreateExceptionFromResponseErrors(httpResponse);
                        }
  
                        if (!httpResponse.IsSuccessStatusCode)
                        {
                          return null;
                        }
  
                     var headers = httpResponse.Headers.ToString();
                     string keyRoute = "ROUTEID=";
                     int posRouteID = headers.IndexOf(keyRoute);
                     string routeID2End = headers.Substring(posRouteID + keyRoute.Length, headers.Length - posRouteID - keyRoute.Length);
                     int semiColumn = routeID2End.IndexOf(";");
                     var routeID = ";" + keyRoute + routeID2End.Substring(0, semiColumn);
                     string dataResult = httpResponse.Content.ReadAsStringAsync().Result;
                     session = JsonConvert.DeserializeObject&lt;SessionSL&gt;(dataResult);
                     session.RouteID = routeID;
                      }
                catch (WebException ex)
                      {
                          var stream = ex.Response.GetResponseStream();
                          using (var sr = new StreamReader(stream))
                          {
                              Console.WriteLine(sr.ReadToEnd());
                          }
                      }
                  }
                  catch (Exception e)
                  {
                      _esito = SendError(_esito, e, Constants.ErrorCode.LoginError);
                  }
  
  
                  return session;
              }
  
  public static HttpRequestMessage PrepareRequest(string host, HttpMethod method, object contentObj)
          {
  
              var jsonObj = JsonConvert.SerializeObject(contentObj);
  
              ServicePointManager.ServerCertificateValidationCallback += delegate (object sender, X509Certificate cert,
                                                                                 X509Chain chain,
                                                                                 SslPolicyErrors sslPolicyErrors)
              { return true; };
  
              StringContent content = new StringContent(jsonObj.ToString(), Encoding.UTF8, "application/json");
  
              var HttpRequest = new HttpRequestMessage()
              {
                  RequestUri = new Uri(host),
                  Method = method,
                  Content = content
              };
  
              return HttpRequest;
  
          }
  </code></pre>
  
  <p>And this is CustomHttpClient class:</p>
  
  <pre><code>public sealed class CustomHttpClient
      {
          private static HttpClient _client;
  
          public static HttpClient GetClient()
          {
              if (_client == null)
              {
                  var messageHandler = new HttpClientHandler()
                  {
                      UseCookies = false,
              };
  
                  //messageHandler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
                  //messageHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =&gt; true;
                  ServicePointManager.ServerCertificateValidationCallback += delegate (object sender, X509Certificate cert,
                                                                                 X509Chain chain,
                                                                                 SslPolicyErrors sslPolicyErrors)
                  { return true; };
  
                  _client = new HttpClient(messageHandler)
                  {
                      Timeout = TimeSpan.FromSeconds(Constants._HTTP_TIMEOUT_SEC),
                      //BaseAddress = new Uri("myendpoint");
                  };
                  _client.DefaultRequestHeaders.Accept.Clear();
                  _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                  _client.DefaultRequestHeaders.Add("Connection", "keep-alive");
              }
  
              return _client;
          }
      }
  </code></pre>
  
  <p>My app is WFP...
  What I do not understand is that the same methods I used to make an app in Xamarin Forms and I never received in the response the error http 500 ...
  And I think there are no errors in the code .. and not even on the server because with tools like Postman I can easily connect and I always receive as a message "200 OK"...</p>