简体   繁体   English

C#RestSharp RestClient对同一API发出100次请求后失败

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

I have a issue when i use RestSharp RestClient. 我在使用RestSharp RestClient时遇到问题。 I try to make multiple request inside a loop but it always failed after the 99th request. 我尝试在一个循环内发出多个请求,但在第99个请求后总是失败。

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?? 为什么它可以工作99次,而我什么都没有了?

EDIT : I have done the same code in JAVA and tried also 120 iterations of the request in POSTMAN and it's working. 编辑:我已经在JAVA中完成了相同的代码,并尝试了POSTMAN中请求的120次迭代,并且它正在工作。 And it's also works with HTTP URL but not HTTPS after 100 times. 而且它也可以使用HTTP URL,但100次后不能使用HTTPS。 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. 执行完请求后,您需要检查此属性是否不为null。 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}");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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