gpt4 book ai didi

c# - WebClient 默认超时?

转载 作者:太空狗 更新时间:2023-10-29 21:26:57 28 4
gpt4 key购买 nike

我看到了来自 https://stackoverflow.com/questions/6262547/webclient-timeout-error 的帖子,它表示默认超时为 100 秒。但我看到来自 How to change the timeout on a .NET WebClient object 的评论说

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

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

最佳答案

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

我在我的本地 PC 上发布了一个简单的网站,该网站在收到请求后等待 300 秒(足以使 WebClient 超时),然后返回响应。

我编写了一个简单的程序,它使用 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);
}
}

WebClient 在100 秒 后超时。该程序产生了这个输出:

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。

关于c# - WebClient 默认超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34083278/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com