开发者

Sending HTTP requests with C# HttpWebRequest or WebClient?

开发者 https://www.devze.com 2023-01-24 08:25 出处:网络
I can use bothHttpWebRequest to send an HTTP request and get an HTTP response without开发者_JS百科 a WebClient.

I can use both HttpWebRequest to send an HTTP request and get an HTTP response without开发者_JS百科 a WebClient.

When should you use HttpWebRequest and when should you use WebClient?


WebClient can be used when you don't need any fine-tuning.

When using HttpWebRequest, you can control various options, including timeouts (very important). So basically - WebClient for toy projects / POCs, HttpWebRequest for actual business.


Personally I always use WebClient. The API seems more simple. It uses HttpWebRequest under the covers.


WebClient is ideal for downloads and uploads.

HttpWebRequest is ideal for web connections, including sending HTTP POST requests, as seen here: HTTP request with post


If you do not need access to the underlying stream but are just uploading or downloading "data", i.e. a file some bytes or a string, WebClient is a simplifying abstraction.

0

精彩评论

暂无评论...
验证码 换一张
取 消