在 C# .NET 中加载第一个连接需要很长时间

发布于 2024-08-04 17:47:30 字数 227 浏览 11 评论 0原文

我正在制作一个连接到网站并从中下载 XML 的程序。然后它向用户显示信息。

我遇到的问题是,当我第一次打开程序并开始下载 XML 信息时,需要很长时间。当我在程序仍然打开的情况下从网站加载另一个页面时,下载需要大约半秒的时间。我想知道是否有什么办法可以避免这种情况。

我目前使用 HttpWebRequest 来下载流,并使用 StreamReader 来读取它。然后我使用 XLINQ 浏览并解析 XML。

I'm making a program that connects to a website and downloads XML from it. It then displays the information to the user.

The problem I am having is when I first open the program and start downloading the XML information it takes a really long time. When I load another page from the site with the program still open, it takes about half a second to download. I was wondering if there was any way to avoid this.

I currently use an HttpWebRequest to download the stream and a StreamReader to read it. Then I go through and parse the XML using XLINQ.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-08-11 17:47:30

尝试显式设置代理。如果您没有定义代理,HttpRequest 类将花时间搜索代理。一旦找到(或尚未找到),它将在应用程序的整个生命周期中使用该信息,从而加快后续请求的速度。

//internally sets "ProxySet" to true, so won't search for a proxy
request.Proxy = null;

您还可以在 .config 中定义它:

<system.net>
  <defaultProxy
    enabled="false"
    useDefaultCredentials="false" >
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>

Try explicitly setting the proxy. If you don't have a proxy defined, the HttpRequest class will spend time searching for one. Once it has (or hasn't) found one, it will use that information for the life of the application, speeding up subsequent requests.

//internally sets "ProxySet" to true, so won't search for a proxy
request.Proxy = null;

You can also define this in the .config:

<system.net>
  <defaultProxy
    enabled="false"
    useDefaultCredentials="false" >
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>
画骨成沙 2024-08-11 17:47:30

第一次延迟可能是由于以下原因组合造成的:

  1. 解析服务器 DNS 条目的时间
  2. 下载代理的时间
    自动配置脚本,编译并
    执行它来确定
    从您的应用程序到代理服务器的有效代理
  3. 网络延迟(如果您的环境中有代理服务器)
  4. 网络延迟
    代理服务器到实际的
    目标服务器。
  5. 延迟时间为
    提供 XML 服务的服务器
    文档。如果它必须遍历一个
    内存中的对象表示和
    生成 XML 文档,即
    可能需要一些时间。另外,如果是
    使用类似的技术
    XML 序列化生成
    文件,然后取决于如何
    配置串行器,第一个
    始终调用序列化/反序列化
    需要很长时间,因为事实上
    中间组件需要
    来生成和编译。
  6. 在客户端解析 XML
    可能需要时间,尤其是如果 XML
    文档结构非常复杂。
  7. 如果XLinq(如XMLSerializer)
    为 XML 生成临时程序集
    解析与查询,然后是第一个
    请求将花费比
    后续的。

要确定哪个部分花费了时间,请使用 System.Diagnostics.Stopwatch() 在代码中插入一些时间记录:

// this is the time to get the XML doc from the server, including the time to resolve DNS, get proxy etc.
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
timer.Stop();
Console.WriteLine("XML download took: " + timer.ElapsedMilliseconds);

timer.Start();
// now, do your XLinq stuff here...
timer.Stop();
Console.WriteLine("XLinq took: " + timer.ElapsedMilliseconds);

您可以围绕此插入一个循环,并查看第一个请求和后续请求之间各个组件的差异。

如果您发现差异在于下载,而不是查询,那么您可以使用 Wireshark 获取网络嗅探来进一步调查

希望这有帮助。

The first time delay can be due to a combination of the following:

  1. Time to resolve the server DNS entry
  2. Time to download the proxy
    autoconfig script, compile and
    execute it to determine the
    effective proxy
  3. network latency from your app to the proxy server (if there is a proxy server in your environment)
  4. network latency from
    the proxy server to the actual
    destination server.
  5. The latency on
    the server to serve the XML
    document. If it has to traverse an
    in-memory object representation and
    generate the XML document, that
    might take some time. Also, if it is
    using techniques like
    XML-Serialization to generate the
    document, then depending on how the
    serializer is configured, the first
    call to serialize/deserialize always
    takes a long time, due to the fact
    that an intermediate assembly needs
    to be generated and compiled.
  6. Parsing the XML on the client side
    might take time, esp if the XML
    document structure is very complex.
  7. If XLinq (like the XMLSerializer)
    generates temp assembly for the XML
    parsing & querying, then the first
    request will take more time than the
    subsequent ones.

To figure out which part is taking time, insert some time logging into your code using System.Diagnostics.Stopwatch():

// this is the time to get the XML doc from the server, including the time to resolve DNS, get proxy etc.
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
timer.Stop();
Console.WriteLine("XML download took: " + timer.ElapsedMilliseconds);

timer.Start();
// now, do your XLinq stuff here...
timer.Stop();
Console.WriteLine("XLinq took: " + timer.ElapsedMilliseconds);

You can insert a loop around this, and see what the difference for the various components between the first request and subsequent requests is.

If you find that the difference is in the downloading, and not the querying, then you can investigate further by getting a network sniff using Wireshark.

Hope this helps.

波浪屿的海角声 2024-08-11 17:47:30

您可能需要做更多研究才能弄清楚请求的哪一部分在第一次传递中花费的时间更长。我的第一直觉是,获取您指定域名的 IP 地址的 DNS 请求花费了更长的时间,因为它在第一次运行时没有被缓存。也可能是另一端的 Web 服务器在您第一次查询时必须运行一些启动脚本。您提到第一个请求需要很长时间,但您没有说需要多长时间。这是否会导致一个大问题,以至于需要很长时间才能执行第一个请求,或者这只是一个烦恼?

You would probably have to do some more research to figure out what part of the request is taking longer on the first pass. My first instinct says that the DNS request to get the IP address for the domain name you specify is taking longer, because it isn't cached the first time it runs. It could also be the web server on the other end that has to run some start-up scripts the first time you query it. You mentioned that the first request takes a long time, but you don't say how long. Is this causing a big problem that it takes so long to do the first request, or is it just an annoyance?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文