WebClient 超时错误 [英] WebClient timeout error

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

问题描述

我创建了一个如下所示的类.

I created a class like below.

public class WebDownload : WebClient
{
    private int _timeout;
    /// <summary>
    /// Time in milliseconds
    /// </summary>
    public int Timeout
    {
        get
        {
            return _timeout;
        }
        set
        {
            _timeout = value;
        }
    }

    public WebDownload()
    {
        this._timeout = -1;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest result = base.GetWebRequest(address);
        result.Timeout = this._timeout;
        return result;
    }
}

当我创建此类的对象时,它会创建一个 webclient 对象并将 timeout 设置为 -1,以便它等待响应的时间不受限制.

When I create an object of this class it creates a webclient object and sets timeout to -1 so that it waits unlimited time for a response.

但即使在我将 timeout 设置为 -1 后,它也会导致超时错误.

But even after I set timeout to -1 it results in a timeout error.

有没有办法解决这个问题?

Is there a solution for this?

推荐答案

我知道你从哪里得到 -1 部分,但在 MSDN 文章中关于 Timeout 它说它会抛出一个 ArgumentOutOfRangeException 如果:

I have ho idea where you got the -1 part from, but in the MSDN article regarding Timeout it says that it will throw an ArgumentOutOfRangeException if:

指定的值小于零并且不是无限的.

The value specified is less than zero and is not Infinite.

默认值是 100,000 毫秒(100 秒).

The default value is 100,000 milliseconds (100 seconds).

还有一点需要考虑:

要指定读取或写入操作超时之前等待的时间量,请使用 ReadWriteTimeout 属性.

To specify the amount of time to wait before a read or write operation times out, use the ReadWriteTimeout property.

域名系统 (DNS) 查询最多可能需要 15 秒才能返回或超时.如果您的请求包含需要解析的主机名,并且您将 Timeout 设置为小于 15 秒的值,则可能需要 15 秒或更长时间才能引发 WebException 以指示您的请求超时.

A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

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

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