在 C# .NET 中加载第一个连接需要很长时间
我正在制作一个连接到网站并从中下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试显式设置代理。如果您没有定义代理,
HttpRequest
类将花时间搜索代理。一旦找到(或尚未找到),它将在应用程序的整个生命周期中使用该信息,从而加快后续请求的速度。您还可以在 .config 中定义它:
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.You can also define this in the .config:
第一次延迟可能是由于以下原因组合造成的:
自动配置脚本,编译并
执行它来确定
从您的应用程序到代理服务器的有效代理
代理服务器到实际的
目标服务器。
提供 XML 服务的服务器
文档。如果它必须遍历一个
内存中的对象表示和
生成 XML 文档,即
可能需要一些时间。另外,如果是
使用类似的技术
XML 序列化生成
文件,然后取决于如何
配置串行器,第一个
始终调用序列化/反序列化
需要很长时间,因为事实上
中间组件需要
来生成和编译。
可能需要时间,尤其是如果 XML
文档结构非常复杂。
为 XML 生成临时程序集
解析与查询,然后是第一个
请求将花费比
后续的。
要确定哪个部分花费了时间,请使用 System.Diagnostics.Stopwatch() 在代码中插入一些时间记录:
您可以围绕此插入一个循环,并查看第一个请求和后续请求之间各个组件的差异。
如果您发现差异在于下载,而不是查询,那么您可以使用 Wireshark 获取网络嗅探来进一步调查。
希望这有帮助。
The first time delay can be due to a combination of the following:
autoconfig script, compile and
execute it to determine the
effective proxy
the proxy server to the actual
destination server.
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.
might take time, esp if the XML
document structure is very complex.
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():
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.
您可能需要做更多研究才能弄清楚请求的哪一部分在第一次传递中花费的时间更长。我的第一直觉是,获取您指定域名的 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?