C# RestSharp ignoring timeout parameter?

随声附和 提交于 2019-12-25 00:58:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!