Share via

Two requests from different threads with different urls in same httpclient instance

Dimitris Christoforidis 301 Reputation points
2021-06-16T07:30:47.757+00:00

I make two different 'GET' requests from two different threads at the same time. Will i have any conflicts if i will try to send at the same time but with different urls?

 static HttpClient client = new HttpClient();

 private void Thread1()
 {
   var result= await client.GetAsync("http://URL1");
 }

 private void Thread2()
 {
   var result= await client.GetAsync("http://URL2");
 }

Methods Thread1 and Thread2 will run at the same time. Will i have any problem? Or httpclient will handle both requests fine?

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

Anonymous
2021-06-17T03:22:39.887+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

Will i have any problem? Or httpclient will handle both requests fine?

No, you can use one HttpClent to 'GET' request several at the same time.

Here is an article from Henrik F. Nielsen about HttpClient where he says:

A single HttpClient can be used to send as many HTTP requests as you want concurrently so in many scenarios you can just create one HttpClient and then use that for >all your requests."

And since .NET 4.5 The following instance methods are thread safe, please refer to the following thread.

https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0#remarks

   CancelPendingRequests  
   DeleteAsync  
   GetAsync  
   GetByteArrayAsync  
   GetStreamAsync  
   GetStringAsync  
   PostAsync  
   PutAsync  
   SendAsync  
   PatchAsync  

Best Regards,

Leon Lu


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.