9.7. 多次尝试版本的getUrlRespHtml:getUrlRespHtml_multiTry

默认的getUrlRespHtml只允许一次,即当出错时,就返回空字符串了,就不再继续了。

此处的getUrlRespHtml_multiTry,是带多次尝试的版本。

其完整代码是:


    public string getUrlRespHtml_multiTry
                                    (string url,
                                    Dictionary<string, string> headerDict = defHeaderDict,
                                    string charset = defCharset,
                                    Dictionary<string, string> postDict = defPostDict,
                                    int timeout = defTimeout,
                                    string postDataStr = defPostDataStr,
                                    int readWriteTimeout = defReadWriteTimeout,
                                    int maxTryNum = defMaxTryNum,
                                    int retryFailSleepTime = defRetryFailSleepTime)          
    {
        string respHtml = "";

        for (int tryIdx = 0; tryIdx < maxTryNum; tryIdx++)
        {
            respHtml = getUrlRespHtml(url, headerDict, charset, postDict, timeout, postDataStr, readWriteTimeout);
            if (!string.IsNullOrEmpty(respHtml))
            {
                break;
            }
            else
            {
                //something wrong
                //maybe network is not stable
                //so wait some time, then re-do it
                System.Threading.Thread.Sleep(retryFailSleepTime);
            }
        }

        return respHtml;
    }

    

例 9.8. getUrlRespHtml_multiTry 的使用范例


    //respHtml = crl.getUrlRespHtml(viewHtmlUrl);
    respHtml = crl.getUrlRespHtml_multiTry(viewHtmlUrl);