1

The documentation about the PostAsync method of the HttpClient is a bit...on the low side.

I'm wondering about the behaviour of it: Does it bring any exceptions in case of timeout or when the Website called throws an exception? If not what happens in These cases?

As example

using (HttpClient Client = new HttpClient())
{
     var result = Client.PostAsync(url, content).Result;
}

The site called sometime throws exceptions or in the case of high traffic times out. I'm not sure what the result is then. Do I get exceptions, "endless Loop" (thus timeouts don't occur), or is just the result empty if exceptions are thrown or a timeout occurs?

Thomas
  • 2,886
  • 3
  • 34
  • 78

1 Answers1

2

As a first note, please avoid using Result. It is a blocking call. You should use the async/await keywords. Mark the corresponding method as async and prepend the method call with await:

var result = await Client.PostAsync(url, content);

Regarding your question, the result of this call is a Task<HttpResponseMessage>. Statuses of a Tasks can be found here TaskStatus Enumeration. Of course this call can fail. So you should catch any exception that may be thrown.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • So even synchronous I would get a timeout? The blocking itself is only for the Client if I'm correct not the webservice itself? (the Client is constructed so that it Needs to wait on a Response in any case so I wouldn't see a great Advantage for an asynchronous call) – Thomas Apr 25 '18 at 08:40
  • @Thomas any call to a web service can timeout either this call is made synchronously or asynchronously. The blocking is related with your application. The web service you call just receives a request and has to respond to it. There isn't any blocking concept at this level. Regarding the advantages of asynchronous programming, please have a look at https://stackoverflow.com/questions/28841345/benefits-of-using-async-and-await-keywords, it has a nice explanation. – Christos Apr 25 '18 at 08:42
  • superb. Wasn't sure as the docu didn't state anything on the reaction of the postasync call. Normally you are right that async / await is definitively the way to go (+1 also for that). In my usecase I only want to continue in the application when the call has ended (one way or the other), so blocking on the clientside is ok for me there. tnx will accept the answer as soon as it is possible to accept. – Thomas Apr 25 '18 at 08:44