Asp.net 对URL的连续调用导致发生异常:远程服务器返回错误:(500)内部服务器错误。
下面是我试图为我需要的应用程序执行的代码片段。我正在记录服务器不会抛出异常的couts 但我提供的计数器(i)一直到170-190记录Asp.net 对URL的连续调用导致发生异常:远程服务器返回错误:(500)内部服务器错误。,asp.net,asp.net-mvc-2,httpwebrequest,webresponse,Asp.net,Asp.net Mvc 2,Httpwebrequest,Webresponse,下面是我试图为我需要的应用程序执行的代码片段。我正在记录服务器不会抛出异常的couts 但我提供的计数器(i)一直到170-190记录 for (int j = 0; j < 1000 ; j++) { i++; WebRequest request = WebRequest.Create(requestUrl); request.Credenti
for (int j = 0; j < 1000 ; j++)
{
i++;
WebRequest request = WebRequest.Create(requestUrl);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
using (WebResponse response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
var htmlString = reader.ReadToEnd();
partialViews.Add(MvcHtmlString.Create(htmlString));
}
}
}
}
for(int j=0;j<1000;j++)
{
i++;
WebRequest=WebRequest.Create(requestUrl);
request.Credentials=CredentialCache.DefaultNetworkCredentials;
使用(WebResponse=request.GetResponse())
{
使用(var stream=response.GetResponseStream())
{
使用(变量读取器=新的流读取器(流))
{
var htmlString=reader.ReadToEnd();
Add(MvcHtmlString.Create(htmlString));
}
}
}
}
我在做什么不正确的事?有没有办法在这样的循环中连续地从不同的应用程序URL获取html字符串
我还尝试使用
WebClient
类来代替WebRequest
和WebResponse
我在不断联系API v(1)时遇到了这个错误。当我连接以检查活动状态时,这似乎是随机发生的。我要做的是睡眠30秒,然后重试通话。我将其设置为一个循环,最多调用10次。我从来没有叫它超过3
您需要捕获GetResponse中的500错误(我只在下面粘贴了我的流程请求代码)。您可能需要根据自己的需要调整此代码:
//this is used to call cc multiple times if it returns 500 errors
public int _iWexCounter;
500错误是什么?嗨,克里斯,没有内部异常。记录的唯一错误消息是“远程服务器返回错误:(500)内部服务器错误”。资料来源:系统
//Process the webrequest
private string ProcessRequest()
{
string sResponse = string.Empty;
try
{
HttpWebResponse ccResponse = (HttpWebResponse)_myWebRequest.GetResponse();
StreamReader reader = new StreamReader(ccResponse.GetResponseStream());
sResponse = reader.ReadToEnd();
// Close Reader
reader.Close();
// Close the response to free up the system resources
ccResponse.Close();
//the connect to CC was good, reset the wex error counter = 0
_iWexCounter = 0;
}
catch (WebException wex)
{
//if the cc server returns a 500, try to connect to it 10 times before blowing it all up
if (wex.Message == "The remote server returned an error: (500) Internal Server Error.")
{
_iWexCounter++;
if (_iWexCounter > 10)
{
// Get the web exception type, error number returned here
sResponse = "Web Exception: " + wex.Status + wex.Message + wex.StackTrace + wex.GetBaseException().Message + wex.GetBaseException().StackTrace + sResponse;
SetConstantContactDifferentialAddTracker(_iConstantContactDifferentialAddTrackerID, "FAILURE", sResponse);
_sMessages.Append(wex.Message + Environment.NewLine);
}
else
{
sResponse = "500 retry";
}
}
}
catch (Exception ex)
{
// Get the web exception type, error number returned here
sResponse = "General Exception: " + ex.Message;
SetConstantContactDifferentialAddTracker(_iConstantContactDifferentialAddTrackerID, "FAILURE", sResponse);
_sMessages.Append(ex.Message + Environment.NewLine);
}
return sResponse;
}