我们在客户端应用程序中有一个代码,它将数据发布到另一个程序上运行的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){}
Run Code Online (Sandbox Code Playgroud)
我们正在高负载环境中对其进行测试,并尝试对请求/响应延迟进行压力测试.如果我将这两个程序放在同一台机器上,我会在一秒钟内从发布应用程序发送大约160条消息到http监听器应用程序,但是如果我将http监听器应用程序放在同一网络上的不同机器上(本地网络)我们在内部创建),这个数字下降到大约5条消息/秒.
以下是我们迄今为止所尝试的内容:
奇怪的是,如果侦听器应用程序位于同一台机器上,则相同的代码正常工作.有谁知道HTTP帖子有任何限制或我忽略的东西?
我发现了一个问题,在这里谈论的WebClient()和HttpWebRequest的.所以基本上WebClient()只是httpwebRequest的包装器.我们决定使用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)
Run Code Online (Sandbox Code Playgroud)
那么我们发现真正让人与众不同的是KeepAlive旗帜.将默认值设置为true时,只要我们将此标志设置为false,即使使用身份验证,整个http post进程也会变得非常快.如果我使用的是WebClient类,那么这个标志不会暴露给我,我认为它默认保持值KeepAlive = true.
希望有人发现这些信息在路上很有用.
| 归档时间: |
|
| 查看次数: |
3360 次 |
| 最近记录: |