Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WebClient.UploadString的HTTP Post在发布到同一网络上的另一台计算机时速度非常慢_C#_Http_Http Post - Fatal编程技术网

C# 使用WebClient.UploadString的HTTP Post在发布到同一网络上的另一台计算机时速度非常慢

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.

我们在下面的客户端应用程序中有一段代码,它将数据发布到另一个程序上运行的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){}
我们正在高负载环境中测试它,并尝试对请求/响应延迟进行压力测试。 如果我把这两个程序放在同一台机器上,我会在一秒钟内从POST应用程序向http listener应用程序发送大约160条消息,但如果我把http listener应用程序放在同一网络(我们在内部创建的本地网络)上的不同机器上,这个数字会下降到大约5条消息/秒

以下是我们迄今为止所做的尝试:

  • 对第二台机器的Ping显示它在不到1ms的时间内响应,并且 tracert显示它只有一个跳跃。服务器之间没有防火墙或代理 两台机器
  • 我使用和来产生大量的流量来发布到 另一台机器上的侦听器应用程序 (大约每秒160条信息)。在我看来,这排除了网络延迟或侦听器应用程序是否存在问题
  • 在发布appand中,我尝试使用UploadStringAsync而不是UploadString,这没有多大区别
  • 没有防病毒ect
  • 奇怪的是,如果listener应用程序在同一台机器上,同样的代码正常工作。有人知道HTTP post有任何限制或我忽略的东西吗

    我在这里找到了一个关于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) 
    
    然后,我们发现真正让情况有所不同的是国旗KeepAlive。当默认值设置为true时,只要我们将此标志设置为false,整个http post过程就变得非常快速,即使使用了身份验证。如果我使用的是WebClient类,那么这个标志不会向我公开,我假设它在默认情况下保持值KeepAlive=true


    希望以后有人会发现这些信息很有用。

    你能尝试禁用身份验证吗?好的,我错了,我再试了一次,现在似乎很快。。。嗯,我会对此进行更多的测试,但我发现很难相信基本身份验证不能快速运行。使用的是什么身份验证?你确定这不是NTLM/谈判吗?另外,你可能想退房