Reputation: 57
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.
Upvotes: 1
Views: 7028
Reputation: 3173
If you are using .net 4.5, you could try the modern http client, which is a nuget package. It makes use of .Net 4.5+ async pattern, which makes very efficient use of your threads.
This code is naive, but should give you a hint of what to do with async
⚠️ Update 2022 - If you're going to be using http clients frequently, you should probably be using HttpClientFactory instead of creating
HttpClient
instances
public async Task<IEnumerable<HttpResponseMessage>> GetStuffs(IEnumerable<string> uris)
{
var tasks = new List<Task<HttpResponseMessage>>();
var client = new HttpClient();
foreach (var uri in uris)
{
var task = client.GetAsync(uri);
tasks.Add(task);
}
await Task.WhenAll(tasks.ToArray());
return tasks.Select(x => x.Result);
}
Upvotes: 2
Reputation: 700432
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.
Upvotes: 1