Maintaining a single instance of the RestSharp client

Unlike this question , is it normal to create a client instance and hold it, or do I need to create a new instance for each call (or call packet)?

+4
source share
3 answers

As long as you need to call the same server to execute reservation requests, it is normal to have only one RestClient and use the same client to create multiple RestRequest.

C # code example:

using (var client = new RestClient("url here")){

// First Call
var request = new RestRequest("API/Path", Method.POST);
request.AddParameter("parameter", "value");
request.AddHeader("header", "value");
var response = client.Execute(request);


// Second Call
var request2 = new RestRequest("API/Path", Method.POST);
request2.AddParameter("parameter", "value");
request2.AddHeader("header", "value");
var response2 = client.Execute(request2);
}

Note: variable client. Which I used twice because it is the base point for both queries. It makes no sense to duplicate it for each request.

Hope this helps.

EDIT: , using .

-2

, .

, IRestRequest.

- qurstion HttpClient :

HttpClient - . A HttpClient HTTP-, HttpClient, .

concurrency, , , - . HttpClient, BCL .NET 4.5 ( NuGet .NET 4.0)

+11

Try:

var client = new RestClient("http://server/");
client.CookieContainer = new System.Net.CookieContainer();

: https://github.com/restsharp/RestSharp/wiki/Cookies

-1

All Articles