skywalker
skywalker

Reputation: 91

Sending HTTP requests with C# HttpWebRequest or WebClient?

I can use both HttpWebRequest to send an HTTP request and get an HTTP response without a WebClient.

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

Upvotes: 7

Views: 5474

Answers (4)

BrokenGlass
BrokenGlass

Reputation: 160852

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.

Upvotes: 0

Evan Mulawski
Evan Mulawski

Reputation: 55334

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

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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

Upvotes: 1

ripper234
ripper234

Reputation: 229988

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.

Upvotes: 3

Related Questions