Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# webclient vs httpclient

public class AHNData
{
    public int i;
    public string str;
}

public class Program
{
    public static HttpClient httpClient = new HttpClient();
    private static readonly string _url = "http://localhost:9000/api/values/";

    public static void Main(string[] args)
    {
       #region "Trace"
       Trace.Listeners.Clear();

       TextWriterTraceListener twtl = new TextWriterTraceListener(
           "C:TempREST_Test.txt");
       twtl.Name = "TextLogger";
       twtl.TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime;

       ConsoleTraceListener ctl = new ConsoleTraceListener(false);
       ctl.TraceOutputOptions = TraceOptions.DateTime;

       Trace.Listeners.Add(twtl);
       Trace.Listeners.Add(ctl);
       Trace.AutoFlush = true;
       #endregion

       int batchSize = 1000;

       ParallelOptions parallelOptions = new ParallelOptions();
       parallelOptions.MaxDegreeOfParallelism = batchSize;

       ServicePointManager.DefaultConnectionLimit = 1000000;

       Parallel.For(0, batchSize, parallelOptions,
           j =>
           {
               Stopwatch sw1 = Stopwatch.StartNew();
               GetDataFromHttpClientAsync<List<AHNData>>(sw1);
           });
       Parallel.For(0, batchSize, parallelOptions,
            j =>
            {
                Stopwatch sw1 = Stopwatch.StartNew();
                GetDataFromHttpClientSync<List<AHNData>>(sw1);
            });
       Parallel.For(0, batchSize, parallelOptions,
            j =>
            {
                using (WebClient client = new WebClient())
                {
                   Stopwatch sw = Stopwatch.StartNew();
                   byte[] arr = client.DownloadData(_url);
                   sw.Stop();

                   Trace.WriteLine("WebClient Sync " + sw.ElapsedMilliseconds);
                }
           });

           Console.Read();
        }

        public static T GetDataFromWebClient<T>()
        {
            using (var webClient = new WebClient())
            {
                webClient.BaseAddress = _url;
                return JsonConvert.DeserializeObject<T>(
                    webClient.DownloadString(_url));
            }
        }

        public static void GetDataFromHttpClientSync<T>(Stopwatch sw)
        {
            HttpClient httpClient = new HttpClient();
            var response = httpClient.GetAsync(_url).Result;
            var obj = JsonConvert.DeserializeObject<T>(
                response.Content.ReadAsStringAsync().Result);
            sw.Stop();

            Trace.WriteLine("HttpClient Sync " + sw.ElapsedMilliseconds);
        }

        public static void GetDataFromHttpClientAsync<T>(Stopwatch sw)
        {
           HttpClient httpClient = new HttpClient();
           var response = httpClient.GetAsync(_url).ContinueWith(
              (a) => {
                 JsonConvert.DeserializeObject<T>(
                    a.Result.Content.ReadAsStringAsync().Result);
                 sw.Stop();
                 Trace.WriteLine("HttpClient Async " + sw.ElapsedMilliseconds);
              }, TaskContinuationOptions.None);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: inheritance 
Csharp :: action c# 
Csharp :: one line condition c# 
Csharp :: round image unity 
Csharp :: f# get last element of list 
Csharp :: non null array length 
Csharp :: calculate textbox value c# 
Csharp :: c# generate insert statement from object 
Csharp :: create a hash of an XML c# 
Csharp :: scaffolding in vs22 asp.net 6 
Csharp :: remove numericUpDown white space 
Csharp :: index list c# 
Csharp :: unity hexmapping 
Csharp :: find the values of dictionaries in C sharp 
Csharp :: C# HttpUtility not found / missing C# 
Csharp :: DisplayUnitType revit api 
Csharp :: c# return statement 
Csharp :: how to combine constructors in c# 
Csharp :: cannot convert from group method to threadstart C# 
Csharp :: blazor wasm roles not working 
Csharp :: mono cast 
Csharp :: c# logical operators 
Csharp :: remove multiple element on list from index a to b C# 
Csharp :: declare a delegate 
Csharp :: c# ienumerable unassigned 
Csharp :: unknown discriminator value mongodb 
Csharp :: c# check if pdf is protected without password 
Csharp :: DataTable GetErrors 
Csharp :: Known Folders C# 
Csharp :: C# Floating Point Literals 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =