簡體   English   中英

C#RestSharp RestClient對同一API發出100次請求后失敗

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

我在使用RestSharp RestClient時遇到問題。 我嘗試在一個循環內發出多個請求,但在第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");
    }
}

我在執行代碼時可以在終端上看到

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 :

為什么它可以工作99次,而我什么都沒有了?

編輯:我已經在JAVA中完成了相同的代碼,並嘗試了POSTMAN中請求的120次迭代,並且它正在工作。 而且它也可以使用HTTP URL,但100次后不能使用HTTPS。 我將其添加到代碼中以查看問題

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

它告訴我

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

不會引發“執行”的異常,但可以在“錯誤異常”響應屬性中使用。 執行完請求后,您需要檢查此屬性是否不為null。 對於您的情況,您需要添加以下內容:

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