WebClient默认超时? [英] WebClient default timeout?

查看:79
本文介绍了WebClient默认超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 https://stackoverflow.com/questions/6262547/webclient-timeout-error ,它表示默认超时为100秒.但是我从如何更改.NET WebClient对象

I see the post from https://stackoverflow.com/questions/6262547/webclient-timeout-error , it says the default timeout is 100 seconds. But I see the comment from How to change the timeout on a .NET WebClient object says

默认超时为100秒.尽管它似乎运行了30秒.–卡特2012年12月13日在16:39

The default timeout is 100 seconds. Although it seems to run for 30 seconds. – Carter Dec 13 '12 at 16:39

在我的程序中,超时总是大约20秒,有人知道原因吗?

In my program the timeout always about 20 seconds, does anybody know the reason?

推荐答案

我整理了一个最小的案例来测试WebClient类的默认超时.

I put together a minimal case to test the WebClient class's default timeout.

我在本地PC上发布了一个简单的网站,该网站在收到请求后等待300秒(足够长的时间使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.

我编写了一个简单的程序,该程序使用WebClient向该站点发出请求,并报告发生的情况:

I wrote a simple program which uses a WebClient to make a request to that site, and report what happens:

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

WebClient在 100秒后超时.该程序产生以下输出:

The WebClient did time out after 100 seconds. 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

客户端测试程序针对.NET Framework 4.6.

The client test program targeted .NET Framework 4.6.

这篇关于WebClient默认超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆