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

Mud*_*san 1 c# http

我正在进行操作超时异常.

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


        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法增加超时属性?请帮忙.

Gon*_*alo 5

最简单的答案是使用client.Timeout = 420000(WebRequest.Timeout),但如果您打算使用计时器,那么您也可以尝试使用异步版本.为webClient.DownloadFile()设置超时的东西