[英]HTTP Post using WebClient.UploadString is very slow when posting to another machine on same network
we have a code below in a client app that posts data to an HTTP listener runs on another program. 我们在客户端应用程序中有一个代码,它将数据发布到另一个程序上运行的HTTP侦听器。
try
{
using (WebClient client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
client.Credentials = new NetworkCredential(NotificationUser, NotificationPassword);
client.UploadString(NotificationUrl, msg); // Notification URL is IP base not DNS name.
}
}
catch (Exception ex){}
We are testing it in a high load environment and try to stress test the request/response latency. 我们正在高负载环境中对其进行测试,并尝试对请求/响应延迟进行压力测试。 If I put both the programs on the same machine, I would get around 160 messages sent in one second from the posting app to the http listener app, but if I put the http listener app on different machine on the same network (a local network we create in house), that number goes down to about 5 messages/second.
如果我将这两个程序放在同一台机器上,我会在一秒内从发布应用程序发送大约160条消息到http监听器应用程序,但是如果我将http监听器应用程序放在同一网络上的不同机器上(本地网络)我们在内部创建),这个数字下降到大约5条消息/秒。
Here are what we have tried so far: 以下是我们迄今为止所尝试的内容:
The weird thing is this same code works normal if the listener app is on the same machine. 奇怪的是,如果监听器应用程序在同一台机器上,则相同的代码正常工作。 Does anyone know that HTTP post has any restriction or something I overlook?
有谁知道HTTP帖子有任何限制或我忽略的东西?
I found a question on here talking about WebClient() and HTTPWebRequest. 我发现了一个问题,在这里谈论的WebClient()和HttpWebRequest的。 So basically WebClient() is just a wrapper around httpwebRequest.
所以基本上WebClient()只是httpwebRequest的包装器。 We decided to test out my code by using HTTPWebRequest class instead.
我们决定使用HTTPWebRequest类来测试我的代码。
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Program.NotificationUrl);
request.KeepAlive = false;
request.Method = "Post";
request.ContentType = "text/xml";
request.Credentials = new System.Net.NetworkCredential(Program.NotificationUser, Program.NotificationPassword);
byte[] data = Encoding.UTF8.GetBytes(msg);
request.ContentLength = data.Length;
Stream reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();
WebResponse response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
reader.ReadToEnd();
}
}
catch (Exception)
Then what we found that really made the different was the flag KeepAlive. 那么我们发现真正让人与众不同的是KeepAlive旗帜。 With a default value set to true, as soon as we set this flag to false, the whole http post process became lightning fast even with the authentication.
将默认值设置为true时,只要我们将此标志设置为false,即使使用身份验证,整个http post进程也会变得非常快。 If I was using WebClient class, this flag is not exposed to me and I assumed it kept the value KeepAlive=true by default.
如果我使用的是WebClient类,那么这个标志不会暴露给我,我认为它默认保持值KeepAlive = true。
Hopefully someone find this information useful down the road. 希望有人发现这些信息在路上很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.