NETCore提供了三种不同类型用于生产的REST API: HttpWebRequest;
WebClient;
HttpClient
,开源社区创建了另一个名为RestSharp的库
。如此多的http库,该怎样选择呢?


然而,强大的个性化操作带来了极大的复杂性。为了简单起见,GET
您需要至少五行代码;
HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://example.com"); WebResponse response = http.GetResponse(); Stream stream = response.GetResponseStream(); using (var streamtemn = File.Create("路径")) { stream.CopyTo(streamtemn); }
如果对http协议不是了如指掌,使用HttpWebRequest会增加你的开发成本,除非你需要非常细节的处理和底层的控制,另外HttpWebRequest库已经过时,不适合业务中直接使用,他更适用于框架内部操作。
0 2WebClient

using (WebClient webClient = new WebClient()) { webClient.DownloadFile("http://example.com", "路径"); }
0
3HttpClient
HttpClient
提供强大的功能,提供了异步支持,可以轻松配合async await 实现异步请求,具体使用可参考:NetCore 2.1中的HttpClientFactory最佳实践




参考:
https://stackoverflow.com/questions/22791376/is-httpwebrequest-or-webclient-faster/22792326#22792326
https://stackoverflow.com/questions/20530152/deciding-between-httpclient-and-webclient
https://social.msdn.microsoft.com/Forums/vstudio/en-US/2ce80a71-1ced-4bcd-adb4-88eef6e6a42d/httpclient-vs-httpwebrequest?forum=wcf

