为什么我不能重复使用 WebClient 来两次发出相同的请求? [英] Why I can't reuse WebClient to make the same request twice?

查看:38
本文介绍了为什么我不能重复使用 WebClient 来两次发出相同的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以代码:

 const long testCount = 2;const string jsonInput = "{\"blockId\":\"1\",\"userId\":\"{7c596b41-0dc3-45df-9b4c-08840f1da780}\",\"sessionId\":\"{46cd1b39-5d0a-440a-9650-ae4297b7e2e9}\"}";秒表手表 = Stopwatch.StartNew();使用 (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.ElapsedMilliseconds/(double)testCount;Console.WriteLine("平均速度:{0} request/ms", testCount);

对于性能测试,我只想多次调用 client.UploadString("http://localhost:8080/BlocksOptimizationServices/dataPositions", "POST", jsonInput).但是在第一个请求之后它总是以

失败<块引用>

远程服务器返回错误:(400) 错误请求."

如果我处理 WebClient 并重新创建 - 有效,但这会增加额外的性能损失..为什么我不能重复使用 WebClient 两次?

解决方案

每次请求后都会清除 WebClient 标头.要亲自查看,您可以添加几个 Debug.Assert() 语句.这与您在第二个请求中的(400) Bad request"错误一致:

for (int i = 0; i 

因此,您可以更改代码以每次设置标题:

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);}

<块引用>

如果我处理 WebClient 并重新创建 - 有效,但这会增加额外的性能损失..为什么我不能重复使用 WebClient 两次?

这里有一个 SO 线程,它似乎并不认为每个请求实例化一个 WebClient 会造成很大的性能损失:WebClient 构建开销

祝你好运!

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?

解决方案

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!

这篇关于为什么我不能重复使用 WebClient 来两次发出相同的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆