/// <summary> /// 直接重试 /// </summary> /// <param name="MethodName"></param> /// <param name="ParameterList"></param> /// <param name="RetryTimes"></param> public static void Direct(DelegateEventParameter MethodName, ArrayList ParameterList, int RetryTimes = 3) { try { var retryTwoTimesPolicy = Policy .Handle <Exception>() .Retry(RetryTimes, (ex, count) => { NLogHelper.ErrorLog(MethodName.Method.Name.ToString() + "重试次数:" + count, ex); }); retryTwoTimesPolicy.Execute(() => { if (ParameterList != null && ParameterList.Count == 1) { MethodName(ParameterList[0].ToString()); } else { NLogHelper.ErrorLog(MethodName.Method.Name.ToString() + "参数异常。"); } }); } catch (Exception ex) { NLogHelper.ErrorLog("重试异常:", ex); } }
/// <summary> /// 发送邮件 /// </summary> /// <param name="emailModel">邮件实体</param> public static void SendEmail(SendEmailModel emailModel) { try { var message = new MimeMessage(); if (emailModel.FromName == string.Empty) { message.From.Add(new MailboxAddress(emailModel.FromAddress)); } else { message.From.Add(new MailboxAddress(emailModel.FromName, emailModel.FromAddress)); } if (emailModel.ToName == string.Empty) { message.To.Add(new MailboxAddress(emailModel.ToAddress)); } else { message.To.Add(new MailboxAddress(emailModel.ToName, emailModel.ToAddress)); } message.Subject = emailModel.Subject; message.Body = new TextPart("plain") { Text = @emailModel.Body }; using (var client = new SmtpClient()) { client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(SmtpServer, SmtpPort, false); client.Authenticate(emailModel.FromAccount, emailModel.FromPassword); client.Send(message); client.Disconnect(true); } } catch (Exception ex) { NLogHelper.ErrorLog(ex.Message, ex); } }
/// <summary> /// POST请求支持重定向 /// </summary> /// <param name="posturl">地址</param> /// <param name="postData">参数</param> /// <param name="contentType">类型[application/json]、[application/x-www-form-urlencoded]</param> /// <returns></returns> public static string Post(string posturl, string postData, string contentType = "application/x-www-form-urlencoded") { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.GetEncoding("utf-8"); byte[] data = encoding.GetBytes(postData); try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = false; request.ProtocolVersion = HttpVersion.Version11; //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; //request.Host = ""; //request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0"; //request.Credentials = CredentialCache.DefaultCredentials; request.Method = "POST"; request.ContentType = contentType; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; return(""); } catch (WebException ex) { NLogHelper.ErrorLog(ex.Message); // 302重定向 return(ex.Response.Headers["Location"].ToString()); } }
/// <summary> /// 直接重试 /// </summary> /// <param name="MethodName"></param> /// <param name="RetryTimes"></param> public static void Direct(DelegateEvent MethodName, int RetryTimes = 3) { try { var retryTwoTimesPolicy = Policy .Handle <Exception>() .Retry(RetryTimes, (ex, count) => { NLogHelper.ErrorLog(MethodName.Method.Name.ToString() + "重试次数:" + count, ex); }); retryTwoTimesPolicy.Execute(() => { MethodName(); }); } catch (Exception ex) { NLogHelper.ErrorLog("重试异常:", ex); } }
/// <summary> /// 间隔重试(无参数)间隔时间5秒、15秒、60秒 /// </summary> /// <param name="MethodName"></param> public static void Interval(DelegateEvent MethodName) { try { var politicaWaitAndRetry = Policy .Handle <Exception>() .WaitAndRetry(new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(60) }, ReportaError); politicaWaitAndRetry.Execute(() => { MethodName(); }); } catch (Exception ex) { NLogHelper.ErrorLog("重试异常:", ex); } }
/// <summary> /// 异常日志 /// </summary> /// <param name="e"></param> /// <param name="tiempo"></param> /// <param name="intento"></param> /// <param name="contexto"></param> private static void ReportaError(Exception e, TimeSpan tiempo, int intento, Context contexto) { NLogHelper.ErrorLog("重试异常:", e); Console.WriteLine($"异常: {intento:00} (调用秒数: {tiempo.Seconds} 秒)\t执行时间: {DateTime.Now}"); }