Browse Source

表单Post类完成retry 3次的机制

Jeff Wang 3 years ago
parent
commit
bc60317094
1 changed files with 39 additions and 8 deletions
  1. 39 8
      FactoryTool_CShare/Http/FormPost.cs

+ 39 - 8
FactoryTool_CShare/Http/FormPost.cs

@@ -18,12 +18,27 @@ namespace MOKA_Factory_Tools
         /// <param name="postParaList"></param>
         /// <param name="isFile">true:用multipart/form-data发送,false:默认格式</param>
         /// <returns></returns>
-        public static string postMessage(string strUrl, int timeout,List<PostDateClass> postParaList, bool isFile = false)
+        public static string postMessage(string strUrl, int timeout, List<PostDateClass> postParaList, bool isFile = false)
         {
             if (isFile == true)
             {
-                bool status;
-                return postFileMessage(strUrl, postParaList,out status, timeout);
+                #region http retry 3次机制;
+                Func<string, List<PostDateClass>, int, string> _postMessage = (url, postlist, timeout) =>
+                {
+                    bool status;
+                    string strReturn = "";
+                    for (int i = 0; i < 3; i++)
+                    {
+                        strReturn = postFileMessage(url, postlist, out status, timeout);
+                        if (status == true)
+                            break;
+                    }
+
+                    return strReturn;
+                };
+
+                return _postMessage(strUrl, postParaList, timeout);
+                #endregion
             }
             else
             {
@@ -38,9 +53,26 @@ namespace MOKA_Factory_Tools
                     strPost.Append("=");
                     strPost.Append(postParaList[i].Value);
                 }
-                return postMessage(strUrl, strPost.ToString());
+
+                #region http retry 3次机制;
+                Func<string, string, string> _postMessage = (url, poststring) =>
+                {
+                    string strReturn = "";
+                    for (int i = 0; i < 3; i++)
+                    {
+                        strReturn = postMessage(url, poststring);
+                        if (strReturn != null)
+                            break;
+                    }
+
+                    return strReturn;
+                };
+
+                return _postMessage(strUrl, strPost.ToString());
+                #endregion
             }
         }
+
         public static string postFileMessage(string strUrl, List<PostDateClass> postParaList,out bool status,int timeout)
         {
             try
@@ -131,9 +163,8 @@ namespace MOKA_Factory_Tools
                 GC.Collect();
                 return e.Message;
             }
-
-
         }
+
         public static string postMessage(string strUrl, string strPost)
         {
             try
@@ -195,6 +226,7 @@ namespace MOKA_Factory_Tools
             }
             return null;
         }
+
         public static string sendMessageCookie(string strUrl, string strPost, CookieContainer cookieContainer)
         {
             try
@@ -254,6 +286,7 @@ namespace MOKA_Factory_Tools
             return null;
         }
     }
+
     class PostDateClass
     {
         String prop;
@@ -287,7 +320,5 @@ namespace MOKA_Factory_Tools
             this.value = value;
             this.type = type;
         }
-
-
     }
 }