Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't reuse WebClient to make the same request twice?

So the code:

        const long testCount = 2;
        const string jsonInput = "{\"blockId\":\"1\",\"userId\":\"{7c596b41-0dc3-45df-9b4c-08840f1da780}\",\"sessionId\":\"{46cd1b39-5d0a-440a-9650-ae4297b7e2e9}\"}";

        Stopwatch watch = Stopwatch.StartNew();

        using (var client = new WebClient())
        {
            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;

            for (int i = 0; i < testCount; i++)
            {
                var response = client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput);
            }

            watch.Stop();
            double speed = watch.ElapsedMilliseconds / (double)testCount; 
            Console.WriteLine("Avg speed: {0} request/ms", testCount);

For performance testing I just want to call client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput) many times. But after the first request it is always fails with

"The remote server returned an error: (400) Bad Request."

If I dispose WebClient and recreate - works, but this add extra performance penalty.. why I can't reuse WebClient twice?

like image 662
Brans Ds Avatar asked Feb 29 '16 15:02

Brans Ds


2 Answers

The WebClient headers are cleared out after each request. To see for yourself, you can add a couple Debug.Assert() statements. This is consistent with your "(400) Bad request" error on the second request:

for (int i = 0; i < testCount; i++)
{
      Debug.Assert(client.Headers.Count == 1); // Content-type is set   
      var response = client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput);    
      Debug.Assert(client.Headers.Count == 0); // Zero!
}

So, you can change your code to set the headers each time:

using (var client = new WebClient())
{            
    for (int i = 0; i < testCount; i++)
    {
        client.Headers["Content-type"] = "application/json";
        client.Encoding = Encoding.UTF8;

        var response = client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput);
    }

If I dispose WebClient and recreate - works, but this add extra performance penalty.. why I can't reuse WebClient twice?

Here's a SO thread that doesn't seem to think instantiating one WebClient per request is much of a performance penalty: WebClient construction overhead

Good luck!

like image 195
Steve Wong Avatar answered Sep 28 '22 07:09

Steve Wong


This may not be what you are looking for, but this is how I got around this.

public WebClient WebClient
                => new WebClient {Headers = new WebHeaderCollection {{HttpRequestHeader.ContentType, "application/json"}}};

This creates a new WebClient each time you use it. I may not be efficient for you to do it this way, but for what I was using this for it works well and removes the need to think about setting the headers for each request.

like image 21
JCisar Avatar answered Sep 28 '22 09:09

JCisar