i was wondering whats the fastest way to make a request and get a response into a string? I read that webclient is basically a helper class for httpwebrequest.
The reason im asking is that for instance, i need to get response from like 2000 url's. And want to make this as efficient as possible.
Just need to know if httpwebrequest is fastest in C# or is there anything else?
Thank you.
It matters very little for performance which method you use to do the request, it's waiting for the server to respond that takes time.
To do multiple requests you can use the asynchronous methods in the WebClient
class. That way you don't have to wait for only one response at a time.
Choose a reasonable number of requests that you want to run at the same time, and use for example the DownloadDataAsync
method to start them. When a response arrives, the DownloadDataCompleted
event (or equivalend depending on what method you use) is triggered. Handle the event to get the downloaded data, and start another request until you have completed them all.
If you are requesting URLs from the same domain, it's usually no gain to request more than a few resources in parallel, if you are requesting them from different domains you can run more of them in parallel.