Why use HttpClient for Synchronous Connection

前端 未结 5 1419
傲寒
傲寒 2020-11-28 01:38

I am building a class library to interact with an API. I need to call the API and process the XML response. I can see the benefits of using HttpClient for Async

5条回答
  •  独厮守ぢ
    2020-11-28 02:34

    I'd re-iterate Donny V. answer and Josh's

    "The only reason I wouldn't use the async version is if I were trying to support an older version of .NET that does not already have built in async support."

    (and upvote if I had the reputation.)

    I can't remember the last time if ever, I was grateful of the fact HttpWebRequest threw exceptions for status codes >= 400. To get around these issues you need to catch the exceptions immediately, and map them to some non-exception response mechanisms in your code...boring, tedious and error prone in itself. Whether it be communicating with a database, or implementing a bespoke web proxy, its 'nearly' always desirable that the Http driver just tell your application code what was returned, and leave it up to you to decide how to behave.

    Hence HttpClient is preferable.

提交回复
热议问题