问题
I have a C# function that checks to see whether or not a proxy is valid by trying to connect to the REST API at https://jsonplaceholder.typicode.com/. However, the RestClient
seems to be ignoring the 5000ms timeout value I gave it, and instead using a (default?) timeout of 2100ms.
Here's my function:
private bool checkProxy(ProxyHolder ph)
{
bool success = false;
String errorMsg = "";
Stopwatch sw = new Stopwatch();
WebProxy wp = new WebProxy(ph.ip, ph.port);
try
{
sw.Start();
IRestResponse response = new RestClient
{
BaseUrl = new System.Uri("https://jsonplaceholder.typicode.com/"),
Timeout = 5000,
ReadWriteTimeout = 5000,
Proxy = wp
}.Execute(new RestRequest
{
Resource = "todos/1",
Method = Method.GET,
Timeout = 5000,
ReadWriteTimeout = 5000,
RequestFormat = DataFormat.Json
});
if (response.ErrorException != null)
{
throw response.ErrorException;
}
success = true;
}
catch (Exception ex)
{
errorMsg = ex.Message;
}
finally
{
sw.Stop();
Console.WriteLine("Success:" + success.ToString() + "|Connection Time:" + sw.Elapsed.TotalSeconds + "|ErrorMsg" + errorMsg);
}
return success;
}
And here's the output from an invalid proxy:
Success:False|Connection Time:21.0209113|ErrorMsgThe operation has timed out
Anyone have an idea on how to fix this?
来源:https://stackoverflow.com/questions/54296820/c-sharp-restsharp-ignoring-timeout-parameter