C# 循环,尝试->;捕获异常,直到它不';T
我有这个:C# 循环,尝试->;捕获异常,直到它不';T,c#,loops,exception,try-catch,httpwebresponse,C#,Loops,Exception,Try Catch,Httpwebresponse,我有这个: return request.GetResponse() as HttpWebResponse; 当网站无法正常工作时,它有时会抛出异常。。(502错误) 这个网站只会关闭几秒钟 所以。。我需要做一个循环并尝试上面的方法,然后捕获异常 我试过这个: while (true) { try { return request.GetResponse() as HttpWebResponse; break; } catch
return request.GetResponse() as HttpWebResponse;
当网站无法正常工作时,它有时会抛出异常。。(502错误)
这个网站只会关闭几秒钟
所以。。我需要做一个循环并尝试上面的方法,然后捕获异常
我试过这个:
while (true)
{
try
{
return request.GetResponse() as HttpWebResponse;
break;
}
catch
{
}
}
但是,这给了我:
在中断时检测到不可访问的代码。
正如其他人所提到的,中断
是冗余的;删除它将消除您的警告。此外,您还应引入一个可恢复的系统,以避免在发生可恢复故障时占用您的系统(并用请求淹没服务器):
double millisecondsDelay = 10;
double delayMultiplyFactor = 2;
int allowedRetries = 10;
while (true)
{
try
{
return request.GetResponse() as HttpWebResponse;
}
catch (Exception e)
{
if (e is /* RecoverableException*/ && allowedRetries-- > 0)
{
Thread.Sleep((int)millisecondsDelay);
millisecondsDelay *= delayMultiplyFactor;
}
else
{
throw;
}
}
}
所以。。我去了,这个:
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;
}
}
}
}
回程充当一个中断,中断是不必要的。使用这样的接球挡也是个坏主意。仅捕获您知道如何处理的特定异常。Thz.:)我要让它整夜运行。。我们要睡觉了P明天见顺便问一下,我如何添加所有4个可能的异常?可以肯定的是P-WebException-InvalidOperationException-NotSupportedException和-ProtocolViolationException是否正确?如果((e是WebException | | e是InvalidOperationException | | e是NotSupportedException | | | e是ProtocolViolationException)&&allowedRetries-->0)@user3712882:是的,尽管我建议将其重构为布尔方法。您也可以参考此Microsoft示例: