如何更改超时在.NET Web客户端对象 [英] How to change the timeout on a .NET WebClient object

查看:220
本文介绍了如何更改超时在.NET Web客户端对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个客户端的数据下载到我的本地计算机(编程)和他们的网络服务器是非常,这是造成我的 Web客户端对象超时很慢。

I am trying to download a client's data to my local machine (programatically) and their webserver is very, very slow which is causing a timeout in my WebClient object.

下面是我的code:

WebClient webClient = new WebClient();

webClient.Encoding = Encoding.UTF8;
webClient.DownloadFile(downloadUrl, downloadFile);

有没有一种方法来设置这个对象上的无限超时?或者,如果没有谁能帮我用一个例子上的另一种方法来做到这一点?

Is there a way to set an infinite timeout on this object? Or if not can anyone help me with an example on an alternate way to do this?

该网址工作正常,在浏览器 - 它只是需要大约3分钟以示

The URL works fine in a browser - it just takes about 3 minutes to show.

推荐答案

您可以延长超时:继承了原WebClient类,并重写的WebRequest吸气设置自己的超时时间,就像下面的例子。 MyWebClient是一个私有类在我的情况

You can extend the timeout: inherit the original WebClient class and override the webrequest getter to set your own timeout, like in the following example. MyWebClient was a private class in my case

  private class MyWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest w = base.GetWebRequest(uri);
            w.Timeout = 20 * 60 * 1000;
            return w;
        }
    }

这篇关于如何更改超时在.NET Web客户端对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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