开发者

WebRequests are slow (taking 4 seconds) how do I speed them up?

I'm doing a web request that is too slow. It takes around 3.2 seconds to GetResponseStream() and .8 seconds to ReadToEnd()

If I run the same request in a web browser I get a response in less than a second... I'm wondering what I can do to speed up the request and what might cause it to be slow?

Based on other questions I saw here, I disabled the proxy in app.config + enabled max connections, just in case (it's still slow). The section I added was:

 <system.net>
<defaultProxy enabled="false">
  <proxy/>
  <bypasslist/>
  <module/>
</defaultProxy>
<connectionManagement>
  <add开发者_StackOverflow社区 address="*" maxconnection="65000" />
</connectionManagement>

Here's a screenshot of what the code looks like and what is slow:

WebRequests are slow (taking 4 seconds) how do I speed them up?

I'd appreciate any help... 4-8 seconds is way too long for a user to wait for an ajax request to complete.

Thanks!


The line

responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

does 3 things at once:

  1. It gets the response from the data.
  2. It gets the response stream from the WebResponse object.
  3. It opens the stream.

To improve timing analysis, and therefore understand where the bottleneck is, you first should recode the section to seperate the tasks.

HttpWebResponse response = WebRequest.GetResponse();
Stream s = response.GetResponseStream();
reader = new StreamReader(s);

In the finally block you just have to close the reader, as this also closes the underlying stream. Instead of explicitly closing the stream you could consider using an using block for the reader.

using (Stream s = response.GetResponseStream()) {
    StreamReader reader = new StreamReader(s);
    responseData = reader.ReadToEnd();
}

I know that this is not a solution but maybe it provides a bit more insight as to where the time is lost.

A final suggestion, consider using asynchroneous execution to reduce the duration of the execution as perceived by the user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜