简体   繁体   English

WebClient默认超时?

[英]WebClient default timeout?

I see the post from https://stackoverflow.com/questions/6262547/webclient-timeout-error , it says the default timeout is 100 seconds. 我从https://stackoverflow.com/questions/6262547/webclient-timeout-error看到帖子,它说默认超时是100秒。 But I see the comment from How to change the timeout on a .NET WebClient object says 但我看到如何更改.NET WebClient对象的超时的评论说

The default timeout is 100 seconds. 默认超时为100秒。 Although it seems to run for 30 seconds. 虽然它似乎运行了30秒。 – Carter Dec 13 '12 at 16:39 - 卡特12年12月13日16:39

In my program the timeout always about 20 seconds, does anybody know the reason? 在我的程序中,超时总是大约20秒,是否有人知道原因?

I put together a minimal case to test the WebClient class's default timeout. 我整理了一个最小的案例来测试WebClient类的默认超时。

I published a simple website to my local PC which, upon receiving a request, waits 300 seconds (long enough to make WebClient time out), and then returns a response. 我向我的本地PC发布了一个简单的网站,在收到请求后,等待300秒(足够长的时间使WebClient超时),然后返回响应。

I wrote a simple program which uses a WebClient to make a request to that site, and report what happens: 我写了一个简单的程序,它使用WebClient向该站点发出请求,并报告发生的情况:

void Main()
{
    Console.WriteLine("Starting request at " + DateTime.Now);
    WebClient client = new WebClient();
    try
    {
        string response = client.DownloadString("http://slowsite.local/");
        Console.WriteLine("Response returned at " + DateTime.Now);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.GetType() + " " + ex.Message + " at " + DateTime.Now);
    }
}

The WebClient did time out after 100 seconds . WebClient在100秒后完成了超时。 The program produced this output: 该程序产生了这个输出:

Starting request at 8/1/2017 9:31:11 AM
System.Net.WebException The request was aborted: The operation has timed out. at 8/1/2017 9:32:51 AM

The client test program targeted .NET Framework 4.6. 客户端测试程序针对.NET Framework 4.6。

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

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