0

以下是我试图为我需要的应用程序执行的代码片段。我正在记录服务器没有抛出异常的 couts 但是我提供的计数器 (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相同的类而不是WebRequestWebResponse

4

1 回答 1

0

我在持续联系 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;
    }
于 2014-09-09T17:22:47.253 回答