加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – HttpWebRequest 64位Windows上的GetResponse延迟

发布时间:2020-12-13 20:20:58 所属栏目:Windows 来源:网络整理
导读:我最近尝试在64位版本的Windows上运行.NET应用程序,并惊讶地发现我所有的HttpWebRequest.GetResponse()对本地网络上的Web服务的调用都在谈论巨大(约500ms)的时间来完成.以下是有关我的测试设置的几个信息: .NET 3.5 Windows Vista Home Premium 32bit,Windo
我最近尝试在64位版本的Windows上运行.NET应用程序,并惊讶地发现我所有的HttpWebRequest.GetResponse()对本地网络上的Web服务的调用都在谈论巨大(约500ms)的时间来完成.以下是有关我的测试设置的几个信息:

> .NET 3.5
> Windows Vista Home Premium 32bit,Windows Vista Business 64bit和Windows Server 2008 64bit.

测试代码是MSDN’s article on HttpWebRequest.GetResponse中描述的示例的稍微修改的版本.我执行的唯一修改只是一个循环,因此我可以使用10个consequtive调用和基本身份验证,因为我定位的Web服务需要身份验证:

using System;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.IO;

public class Program
{
    // Specify the URL to receive the request.
    public static void Main (string[] args)
    {
        CredentialCache crCache = null;
        Stopwatch s = new Stopwatch();
        for (int i = 0; i < 10; i++)
        {
            s.Reset();
            s.Start();
            HttpWebRequest request = 
                (HttpWebRequest)WebRequest.Create (args[0]);

            // Set some reasonable limits on resources used by this request
            request.MaximumAutomaticRedirections = 4;
            request.MaximumResponseHeadersLength = 4;
            // Set credentials to use for this request.
            if (crCache == null)
            {
                crCache = new CredentialCache();
                crCache.Add(new Uri(args[0]),"Basic",new NetworkCredential("user","password"));
            }
            request.Credentials = crCache;
            request.PreAuthenticate = true;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine("Content length is {0}",response.ContentLength);
            Console.WriteLine("Content type is {0}",response.ContentType);

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream,Encoding.UTF8);

            Console.WriteLine("Response stream received.");
            //Console.WriteLine (readStream.ReadToEnd ());
            response.Close();
            readStream.Close();
            s.Stop();
            Console.WriteLine("Request took: " + s.ElapsedMilliseconds);
        }
    }
}

我编译了针对Windows Vista Home Premium的x86和64位Windows机器的x64.这三个机器与托管Web服务的机器连接在同一个网络交换机上.以下是我得到的结果:

> Windows Vista Home Premium 32bit,x86程序集:GetResponse()的第一次调用在大约150ms内完成,而所有连续的调用大概需要10ms.
> Windows Vista Business 64bit,Server 2008 64位,x86和x64程序集:第一次调用大约需要1000ms,而每个连续的调用在500ms内完成.如果我禁用HttpWebRequest.PreAuthenticate,每个GetResponse在1000ms内完成(这是非常合理的,因为在这种情况下,每个请求都会触发两个单独的HTTP请求,一个以非授权结尾,一个得到适当的响应).

有没有人有一个线索的原因,我得到这样长的GetResponse延迟64位版本的Windows?

编辑

有关问题的其他信息:

>当使用Firefox 3.5执行请求时,我测量了响应时间(通过firebug),并且所有机器的请求 – 响应延迟是相同的,即不会重现问题.
>我用Wireshark做了进一步的包分析,并得出以下结果:

32bit:tcp对话如下:

> host->服务器新的Web请求HTTP GET / HTTP / 1.1
> server->主机两个TCP段(?5ms)
> host->服务器Tcp Ack(确认两个段)(?10us delta)
> server->主机HTTP / 1.1 200 OK(?800us delta)
> host->服务器新的Web请求HTTP GET / HTTP / 1.1&捎带TCP前一段的TCP ack(HTTP / 1.1 200 OK)(?10ms delta)

64位:tcp对话如下:

> host->服务器新的Web请求HTTP GET / HTTP / 1.1
> server->主机两个TCP段(?5ms delta,与32bit相同)
> host->服务器Tcp Ack(确认两个段)(?10us delta,与32位相同)
> server->主机HTTP / 1.1 200 OK(?800us delta,与32位相同)
> host->服务器TCP前一帧的TCP ack(HTTP / 1.1 200 OK)(!!! 96ms)
> host->服务器新的Web请求HTTP GET / HTTP / 1.1(!!! 309ms)

在64位机器上获得的500ms主要发生在最后两个步骤中.请注意,这绝对不是与TCP堆栈相关(因为一切都可以用firefox).我们在最后两个步骤中得到不同TCP Ack模式的原因(捎带在32位,而64位分开的TCP Ack帧)是64位情况下新的Web请求延迟了309 96ms(因此TCP堆栈输出一个单独的Ack框架,它不能等待应用程序层).

所以,它似乎是:

>问题是由完成Web请求和发出新请求之间的增量时间引起的.
>该问题与.NET Framework有关系? (绝对不是TCP相关).
>问题发生在从MSDN(MS不会错误吗?)的库存Microsoft代码.

任何线索?

只是遇到这个线程的任何人,并有问题.

64位系统延迟的原因是WebClient等待Windows返回代理值.

编写这样的代码来克服这个问题.

WebClient wc = New WebClient;
wc.Proxy = null;

这将消除一些用户(包括我自己:)看到的延迟)

很高兴我终于可以回馈社区帮助我这么多:)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读