简体   繁体   中英

C# RestSharp RestClient failed after 100 request to the same API

I have a issue when i use RestSharp RestClient. I try to make multiple request inside a loop but it always failed after the 99th request.

for (int i = 0; i < 120; i++)
{
    Console.WriteLine($"Count : {i}");
    try
    {
        var request = new RestRequest("/endpoint/0016GMLCLT00000007456", Method.GET)
        {
            RequestFormat = DataFormat.Json,
            JsonSerializer = new JsonDeserializer()
        };

        request.AddParameter("accepteEmail", "true");

        var response = RestClient.Execute<AuthenticateResponse>(request);

        Console.WriteLine($"API TEST : {response.Content}");
    }
    catch
    {
        Console.WriteLine($"API TEST : FAIL");
    }
}

I can see this on my terminal when i execute the code

Count : 97
API TEST : {"response":"YES","description":"you did it"}
Count : 98
API TEST : {"response":"YES","description":"you did it"}
Count : 99
API TEST :
Count : 100
API TEST :
Count : 101
API TEST :

Why it's working 99 times and after i have nothing??

EDIT : I have done the same code in JAVA and tried also 120 iterations of the request in POSTMAN and it's working. And it's also works with HTTP URL but not HTTPS after 100 times. And i add that to the code to see the problem

 Console.WriteLine($"API TEST : {response.ErrorMessage}");

And it tells me

 The underlying connection was closed: An unexpected error occurred on a send

Exceptions from 'Execute' are not thrown but are available in the 'ErrorException' response property. And you need to check that this property is not null after executing the request. For your case you need to add something like this:

var response = RestClient.Execute<AuthenticateResponse>(request);

if (response.ErrorException == null)
{
    Console.WriteLine($"API TEST : {response.Content}");
}
else
{
    Console.WriteLine($"API TEST : FAIL {response.ErrorException.Message}");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM