11

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

public static async Task<GenerricApiResponse> ProcessWebRequest<T>(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

5
  • does this happen when the method is only called once, or if you have multiple calls to it?. If it is the second it might be possible that you just run out of available sockets for the clients. That's why a httpClient should be a singleton which gets injected Commented Feb 14, 2020 at 14:40
  • Same method call to some other api and it works. looks like there is problem in response. Commented Feb 14, 2020 at 14:49
  • and the response of the request is not chunked right? Because you add it to the request header but do you validate that the server is respecting that? Commented Feb 14, 2020 at 14:57
  • please suggest what correction needed Commented Feb 14, 2020 at 15:04
  • Perhaps try altering the code to match the pattern described here. learn.microsoft.com/en-us/dotnet/architecture/microservices/… you can also remove the 'using' block Commented Feb 18, 2020 at 11:30

1 Answer 1

0

I also get this exception. after investigation, by default HttpClient wait response header + content ready for all methods, eg. PostAsync, SendAsync, GetAsync. so if response has no/invalid content-length. you will get this error. see this. so it is recommend to use SendAsync(xxx, HttpCompletionOption.ResponseHeadersRead)

using (var req = new HttpRequestMessage(HttpMethod.Post, url))
{
  var body = File.ReadAllText("body.txt").Trim();

  req.Content = new StringContent(body, Encoding.UTF8, "application/json-patch+json");

  // the follow code throw exception
  // System.IO.IOException: The response ended prematurely
  // since 'HttpCompletionOption.ResponseContentRead' is the default behavior
  // var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseContentRead);

  var response = await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);

  response.EnsureSuccessStatusCode();
}
Sign up to request clarification or add additional context in comments.

2 Comments

if want to read response content for wrong content-length, see this stackoverflow.com/questions/59590012/…
PostAsync and GetAsync they are use SendAsync in Background

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.