コード例 #1
0
ファイル: HttpMultiClient.cs プロジェクト: EmilyEdna/Synctool
 public List <byte[]> RunBytes(Action <CookieContainer, Uri> Container = null, int RetryTimes = 3, int IntervalTime = 10)
 {
     if (Client == null)
     {
         throw new NullReferenceException("Client未构建请先调用Build()方法");
     }
     try
     {
         return(SyncStatic.DoRetryWait(() =>
         {
             List <Byte[]> Result = new List <Byte[]>();
             MultiConfig.NodeOpt.ForEach(item =>
             {
                 if (item.CacheNode)
                 {
                     var Data = Caches.RunTimeCacheGet <Byte[]>(item.NodePath.ToMd5());
                     if (Data == null)
                     {
                         Result.Add(RequestBytes(item, Container));
                         Caches.RunTimeCacheSet(item.NodePath.ToMd5(), Result.FirstOrDefault(), CacheSecond, true);
                     }
                     else
                     {
                         Result.Add(Data);
                     }
                 }
                 else
                 {
                     Result.Add(RequestBytes(item, Container));
                 }
             });
             return Result;
         }, (ex, count, tiems) =>
         {
             if (count == tiems)
             {
                 Dispose();
             }
         }, RetryTimes, IntervalTime));
     }
     catch
     {
         return(new List <byte[]>());
     }
     finally
     {
         Dispose();
     }
 }
コード例 #2
0
 /// <summary>
 /// 返回Byte
 /// </summary>
 /// <param name="action"></param>
 /// <param name="RetryTimes"></param>
 /// <param name="IntervalTime"></param>
 /// <returns></returns>
 public async Task <List <byte[]> > RunByteAsync(Action <RestResponse> action = null, int RetryTimes = 3, int IntervalTime = 10)
 {
     try
     {
         return(await SyncStatic.DoRetryWait(async() =>
         {
             RestClient client = new RestClient(this.Options);
             List <byte[]> Result = new List <byte[]>();
             foreach (RestNode node in OptionBuilder.Nodes)
             {
                 if (node.UseCache)
                 {
                     var key = node.Route.ToMd5();
                     var result = Caches.RunTimeCacheGet <byte[]>(key);
                     if (result == null)
                     {
                         var response = await ConfigRequest(client, node, action);
                         await Caches.RunTimeCacheSetAsync(key, response, node.CacheSpan);
                         Result.Add(result);
                     }
                     else
                     {
                         Result.Add(result);
                     }
                 }
                 else
                 {
                     var response = await ConfigRequest(client, node, action);
                     Result.Add(response);
                 }
             }
             return Result;
         }, (ex, count, span) =>
         {
             if (RetryTimes == count)
             {
                 Dispose();
             }
         }, RetryTimes, IntervalTime));
     }
     catch (Exception ex)
     {
         throw ex;
     }
     finally
     {
         Dispose();
     }
 }
コード例 #3
0
 internal void SetHeader()
 {
     SyncStatic.TryCatch(() =>
     {
         if (Headers != null && Headers.Count > 0)
         {
             MultiConfig.HeaderOpt.Add(Headers);
         }
         else if (!HeaderKey.IsNullOrEmpty())
         {
             MultiConfig.HeaderOpt.Add(new Dictionary <string, string> {
                 { HeaderKey, HeaderValue }
             });
         }
         else
         {
             throw new Exception("Header配置不满足!");
         }
     }, ex => throw ex);
 }
コード例 #4
0
 internal void SetNode()
 {
     SyncStatic.TryCatch(() =>
     {
         if (NodePath.IsNullOrEmpty())
         {
             throw new Exception("请求地址不能为空!");
         }
         URI = new Uri(NodePath);
         if (!JsonParam.IsNullOrEmpty() && ReqType != MultiType.DELETE && ReqType != MultiType.GET)
         {
             Contents = new StringContent(JsonParam);
             Contents.Headers.ContentType = new MediaTypeHeaderValue("application/json");
         }
         if (!JsonParam.IsNullOrEmpty() && (ReqType == MultiType.DELETE || ReqType == MultiType.GET))
         {
             URI = new Uri(NodePath + JsonParam.ToModel <JObject>().ByUri());
         }
         if (FormParam != null && FormParam != null && FormParam.Count > 0 && ReqType != MultiType.DELETE && ReqType != MultiType.GET)
         {
             Contents = new FormUrlEncodedContent(FormParam);
             Contents.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
         }
         if (FormParam != null && FormParam != null && FormParam.Count > 0 && (ReqType == MultiType.DELETE || ReqType == MultiType.GET))
         {
             URI = new Uri(NodePath + FormParam.ByUri());
         }
         if (EntityParam != null && ReqType != MultiType.DELETE && ReqType != MultiType.GET)
         {
             Contents = new FormUrlEncodedContent(MultiKeyPairs.KeyValuePairs(EntityParam, MapFied));
             Contents.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
         }
         if (EntityParam != null && (ReqType == MultiType.DELETE || ReqType == MultiType.GET))
         {
             URI = new Uri(NodePath + EntityParam.ByUri());
         }
         MultiConfig.NodeOpt.Add(this);
     }, ex => throw ex);
 }
コード例 #5
0
ファイル: CookieOption.cs プロジェクト: EmilyEdna/Synctool
 internal CookieContainer SetCookie(CookieContainer Container)
 {
     return(SyncStatic.TryCatch(() =>
     {
         if (Container == null)
         {
             Container = new CookieContainer();
         }
         if (InstanceCookie)
         {
             return Container;
         }
         else
         {
             if (!URI.IsNullOrEmpty() && Cookies != null && Cookies.Count > 0)
             {
                 Cookies.ForDicEach((key, val) =>
                 {
                     Container.Add(new Uri(URI), new Cookie(key, val));
                 });
                 return Container;
             }
             else if (!URI.IsNullOrEmpty() && CookieColl != null && CookieColl.Count > 0)
             {
                 Container.Add(new Uri(URI), CookieColl);
                 return Container;
             }
             else if (!CookieName.IsNullOrEmpty() && !CookieValue.IsNullOrEmpty() && !CookiePath.IsNullOrEmpty())
             {
                 Container.Add(new Cookie(CookieName, CookieValue, CookiePath, CookieDomain));
                 return Container;
             }
             else
             {
                 throw new Exception("Cookie配置不满足!");
             }
         }
     }, ex => throw ex));
 }