Brans Ds Brans Ds - 1 month ago 6
C# Question

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?

Answer

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!