当前位置: 首页 > 知识库问答 >
问题:

循环,尝试->捕获异常,直到它不

燕经国
2023-03-14
return request.GetResponse() as HttpWebResponse;

我试过这个:

while (true)
{
    try
    {
        return request.GetResponse() as HttpWebResponse;
        break; 
    }
    catch
    {

    }
}

共有1个答案

司寇安宜
2023-03-14

所以..我去了这个这个:

while (true)
        {
            try
            {
                return request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            {
                if (e is WebException && allowedRetries-- > 0)
                {
                    System.Console.WriteLine("Trying to Reconnect...");
                    Thread.Sleep((int)millisecondsDelay);
                    //millisecondsDelay *= delayMultiplyFactor;
                }
                else
                {
                    throw;
                }
            }
        }

但它卡在了“试图重新连接”上...“如果我重新启动它..它立即连接起来。

整个功能..如果有帮助:

public static HttpWebResponse Request (string url, string method, NameValueCollection data = null, CookieContainer cookies = null, bool ajax = true)
    {
        HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest;

        request.Method = method;

        request.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
        request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
        request.Host = "steamcommunity.com";
        request.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11";
        request.Referer = "http://steamcommunity.com/trade/1";

        if (ajax)
        {
            request.Headers.Add ("X-Requested-With", "XMLHttpRequest");
            request.Headers.Add ("X-Prototype-Version", "1.7");
        }

        // Cookies
        request.CookieContainer = cookies ?? new CookieContainer ();

        // Request data
        if (data != null)
        {
            string dataString = String.Join ("&", Array.ConvertAll (data.AllKeys, key =>
                String.Format ("{0}={1}", HttpUtility.UrlEncode (key), HttpUtility.UrlEncode (data [key]))
            )
            );

            byte[] dataBytes = Encoding.ASCII.GetBytes (dataString);
            request.ContentLength = dataBytes.Length;

            Stream requestStream = request.GetRequestStream ();
            requestStream.Write (dataBytes, 0, dataBytes.Length);
        }

        // Get the response
        //return request.GetResponse () as HttpWebResponse;

        //EXCEPTION8712905

        double millisecondsDelay = 2000;//10
        //double delayMultiplyFactor = 2;
        int allowedRetries = 10000;//10

        while (true)
        {
            try
            {
                return request.GetResponse() as HttpWebResponse;
            }
            catch (Exception e)
            {
                if (e is WebException && allowedRetries-- > 0)
                {
                    System.Console.WriteLine("Trying to Reconnect...");
                    Thread.Sleep((int)millisecondsDelay);
                    //millisecondsDelay *= delayMultiplyFactor;
                }
                else
                {
                    throw;
                }
            }
        }
    }
 类似资料:
  • 我希望程序在捕获异常时重新执行 while 循环 - 异常正在接收文本输入。相反,它继续使用下面的代码进行一段时间循环,我希望它再次要求用户输入。 输出:

  • 问题内容: 下面的代码询问用户他/她想要多少个赛车手。 如果在代码中输入数字,则代码会跳出循环,并且程序的其余部分可以正常运行;但是,当我输入诸如“ awredsf”之类的内容时,它应该捕获该异常,并且确实如此。它没有连续提示用户,而是连续循环,这对我来说没有意义。 连续循环时,程序打印如下: 多少赛车手应该参加比赛?多少赛车手应该参加比赛?多少赛车手应该参加比赛?多少赛车手应该参加比赛?多少赛车

  • 我试图在while循环中为“未找到文件”异常编写代码,以便程序继续提示用户输入文件(test.txt)。我在while循环中编写了一个try/catch块。但是,当我删除输入文件(test.txt)时,程序应捕获此错误并打印“error,找不到'test.txt'文件,请重试:”并允许用户输入另一个文件名。然而,该程序崩溃,并给我一个FileNotFoundException。

  • 问题内容: 我有一个看起来像这样的循环: 这是方法的主要内容,其唯一目的是返回浮点数数组。我希望此方法在出现错误时返回,因此我将循环放在一个块中,如下所示: 但是后来我也想到将块放入循环中,如下所示: 是否出于某种原因(无论是性能还是其他原因)偏爱一个? 编辑: 共识似乎是将循环放在try / catch中(可能在其自己的方法中)更干净。但是,仍然存在关于哪个更快的争论。有人可以测试一下并返回统一

  • 问题内容: 我有一个while循环,应该捕获一个非整数输入,并要求用户重新输入一个整数。但是,它只是无限循环错误消息。有谁知道为什么不允许第二次扫描仪输入? 问题答案: 当您输入的令牌不是数字时,调用nextInt不会将扫描仪移到令牌之外。结果,它只是一遍又一遍地读取相同的令牌。 这是nextInt的文档:http : //download.oracle.com/javase/6/docs/api

  • 我真的不知道这个问题。。。 如果数字不正确,块将捕获异常,当我输入-1或0时,它将捕获异常并要求我再次输入数字。。。但如果我键入类似asdasd的内容,它将运行一个无限循环。