C#RestSharp忽略超时参数?

时间:2019-01-21 19:44:33

标签: c# restsharp

我有一个C#函数,它通过尝试连接到https://jsonplaceholder.typicode.com/上的REST API来检查代理是否有效。但是,RestClient似乎忽略了我给它的5000ms超时值,而是使用2100ms(默认值)超时。

这是我的职能:

    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;
    }

这是无效代理的输出:

  

成功:否| 连接时间:21.0209113 | ErrorMsg操作有   超时

有人知道如何解决此问题吗?

0 个答案:

没有答案