1

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.

user3603865
  • 57
  • 1
  • 8
  • 1
    Whatever method you use to instantiate the request or handle the response is going to be a lot faster than actually evaluating the request / response over the network--that will always be your limiting factor. Just make sure you do it in parallel so you're not waiting on one request to finish before you start your next request. – p.s.w.g Oct 16 '14 at 12:44

2 Answers2

2

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);
}
Jon Bates
  • 3,055
  • 2
  • 30
  • 48
  • It gave me idea's just like what Guffa wrote. Thanks. – user3603865 Oct 16 '14 at 13:13
  • How does it answer OP's question "httpwebrequest is fastest in C# or is there anything else?"? – xmojmr Oct 18 '14 at 08:36
  • 1
    Well ultimately, most of the waiting will be in network transmission delay, but the answer clearly states that there is something else. HttpWebRequest has been obsoleted by MS in favour of HttpClient, which makes use of async to allow trivial multi-operation tricks, for performance. I'm not really sure of your point. – Jon Bates Oct 18 '14 at 15:06
1

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Its from a single domain, i guess ill just have to cache them all once, and update the specific cached data when the person wants to view a specific data. Thank you. – user3603865 Oct 16 '14 at 13:12