Пример #1
0
 public Task <byte[]> MakeRequestAsync(Uri uri, string postJson = null, IEnumerable <KeyValuePair <string, object> > headers = null)
 {
     if (uri.Host.IndexOf("localhost", StringComparison.OrdinalIgnoreCase) >= 0 || uri.Host.Contains("127.0.0.1") || uri.Host.Contains("::1"))
     {
         Interlocked.Increment(ref localRequestCount);
     }
     else if (DisableLiveRequests)
     {
         throw new InvalidOperationException("Live requests have been disabled, cannot process url " + uri.ToString());
     }
     else
     {
         Interlocked.Increment(ref liveRequestCount);
     }
     using (WebClient client = new WebClientWithTimeout())
     {
         Assembly versionAssembly = Assembly.GetEntryAssembly();
         if (versionAssembly == null)
         {
             versionAssembly = Assembly.GetAssembly(Type.GetType("IPBanService"));
             if (versionAssembly == null)
             {
                 versionAssembly = GetType().Assembly;
             }
         }
         client.UseDefaultCredentials = true;
         client.Headers["User-Agent"] = versionAssembly.GetName().Name;
         client.Proxy = Proxy ?? client.Proxy;
         if (DisableLiveRequests)
         {
             client.Headers["Cache-Control"] = "no-cache";
         }
         else
         {
             client.CachePolicy = (CachePolicy ?? client.CachePolicy);
         }
         if (headers != null)
         {
             foreach (KeyValuePair <string, object> header in headers)
             {
                 client.Headers[header.Key] = header.Value.ToHttpHeaderString();
             }
         }
         if (string.IsNullOrWhiteSpace(postJson))
         {
             return(client.DownloadDataTaskAsync(uri));
         }
         client.Headers["Content-Type"] = "application/json";
         return(client.UploadDataTaskAsync(uri, "POST", Encoding.UTF8.GetBytes(postJson)));
     }
 }