简体   繁体   English

使用WebClient下载时操作超时错误

[英]Operation timed out error when downloading with WebClient

I am getting Operation timed out exception . 我收到“操作超时”异常。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;

namespace Timer1
{
    class Program
    {
        static void Main(string[] args)
        {

            Timer t = new Timer(TimerCallback, null, 0, 420000); // 7 minutes

        }
        private static void TimerCallback(Object o)
        {


            string url = string.Empty;
            WebClient client = new WebClient();
            url = @"http://myurl.com";
            client.DownloadString(url);


        }
    }
}

Is there a way to increase timeout property ? 有没有办法增加超时属性? Please help. 请帮忙。

The simplest answer is to use client.Timeout = 420000 ( WebRequest.Timeout ), but if you are going to do that with a timer, you might as well try an asynchronous version. 最简单的答案是使用client.Timeout = 420000WebRequest.Timeout ),但是如果要使用计时器进行操作,则最好尝试使用异步版本。 Something along the lines of Set timeout for webClient.DownloadFile() 为webClient.DownloadFile()设置超时

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

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