System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The response ended prematurely
10:56 14 Feb 2020

I am using following code to call some http api's

public static async Task ProcessWebRequest(string url, T req, ILog Logger, int timeoutInSeconds = 0)
{
  var obj = new GenerricApiResponse();
  try
  {
    using (var client = new HttpClient())
    {
      var jsonRequest = JsonSerializer.Serialize(req);
      client.DefaultRequestHeaders.ExpectContinue = false;
      var content = new StringContent(jsonRequest);
      content.Headers.Clear();
      content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

      if (timeoutInSeconds > 0)
      {
        client.Timeout = new TimeSpan(0, 0, timeoutInSeconds);
      }
      var response = await client.PostAsync(url, content);
      obj.HttpResponseCode = response.StatusCode;

      try
      {
        string responsecontent = null;
        responsecontent = response.Content.ReadAsStringAsync().Result;
        if (response.Content != null && response.Content.Headers != null)
        {
          obj.ResponseContentType = response.Content.Headers.ContentType.MediaType;
          if (responsecontent != null && obj.ResponseContentType == "text/html")
          {
            if (responsecontent != null && responsecontent.Length > 1000)
            {
              responsecontent = responsecontent.Substring(0, 1000) + "...";
            }
          }
        }

        obj.Response = responsecontent;
      }
      catch
      {
        obj.IsError = true;
      }
    }
  }
  catch (Exception ex)
  {
    if (ex.InnerException is TimeoutException)
    {
      ex = ex.InnerException;
    }

    obj.IsError = true;
    obj.Exception = ex;
    obj.Response = ex.Message;
  }
  return obj;
}

But getting error

System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: The response ended prematurely.

Any idea what is missing in my code or what wrong i am doing?

Still getting it. Even i have passed 90 seconds as timeout but no effect. Strange thing is some time it did work

c# httprequest httpclient .net-core-3.1