以下是我试图为我需要的应用程序执行的代码片段。我正在记录计数,直到服务器没有抛出异常但是我提供的计数器(i)显示到170-190条记录。
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));
}
}
}
}
我在做什么不正确的事情?有没有办法在这样的循环中连续地从不同的应用程序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;
…
//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;
}