C# 使用WebClient.UploadString的HTTP Post在发布到同一网络上的另一台计算机时速度非常慢
我们在下面的客户端应用程序中有一段代码,它将数据发布到另一个程序上运行的HTTP侦听器C# 使用WebClient.UploadString的HTTP Post在发布到同一网络上的另一台计算机时速度非常慢,c#,http,http-post,C#,Http,Http Post,我们在下面的客户端应用程序中有一段代码,它将数据发布到另一个程序上运行的HTTP侦听器 try { using (WebClient client = new WebClient()) { client.Encoding = System.Text.Encoding.UTF8; client.Credentials = new NetworkCredential(NotificationUser, NotificationPassword); client.
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){}
我们正在高负载环境中测试它,并尝试对请求/响应延迟进行压力测试。
如果我把这两个程序放在同一台机器上,我会在一秒钟内从POST应用程序向http listener应用程序发送大约160条消息,但如果我把http listener应用程序放在同一网络(我们在内部创建的本地网络)上的不同机器上,这个数字会下降到大约5条消息/秒
以下是我们迄今为止所做的尝试:
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)
然后,我们发现真正让情况有所不同的是国旗KeepAlive。当默认值设置为true时,只要我们将此标志设置为false,整个http post过程就变得非常快速,即使使用了身份验证。如果我使用的是WebClient类,那么这个标志不会向我公开,我假设它在默认情况下保持值KeepAlive=true
希望以后有人会发现这些信息很有用。你能尝试禁用身份验证吗?好的,我错了,我再试了一次,现在似乎很快。。。嗯,我会对此进行更多的测试,但我发现很难相信基本身份验证不能快速运行。使用的是什么身份验证?你确定这不是NTLM/谈判吗?另外,你可能想退房