When you use HttpWebRequest to establish an HTTP request, the first connection is slow to respond, and a request timeout error occurs, and there is approximately more than 10 seconds to wait, but once the first run is successful, the following request page will be fast.
Online search, and found that I encountered the same problem with a lot of netizens.
Netizen Xiao A said:
"The first time you use HttpWebRequest to request a connection is very slow, but as soon as the first connection GetResponse or GetRequestStream () succeeds, the back is fast. However, as soon as a redial, it will take a long time to get GetResponse () or GetRequestStream ().
Ask how to solve this problem, even if the first time slow, but after the network redial, you can quickly link to get the response.
Use the loop to apply his first time super slow, and then followed quickly, but as long as the network connection after the reconnection after the first time is very slow. Solution ~ ~ ~ "
Netizen Small B said:
"Today I upgraded my previous program to a. NET 4.0 version, which has a section of code that gets HTML through HttpWebRequest, which runs very slowly for the first time and runs faster." A search on the internet, found the reason:. The default agent in NET4.0 is turned on, and when the default agent is used, the proxy is bypassed only after the wait timeout, causing the first connection to be particularly slow. (Don't know why Microsoft wants to open this) "
"Where is the problem?"
. The default proxy in NET4.0 or 3.5 is on, and I'm not setting it! It is blocked because the proxy will not be bypassed until the wait times out.
Resources
"It ' s not set at all in App.cong or Machine.config." Hmm. If I ' m reading the
MSDN docs Right, the default to Defaultproxy.enabled is TRUE if the element
Isn ' t specified at all. That would is consistent with my observations.
"
Http://msdn2.microsoft.com/en-us/library/kd3cf2ex (vs.80). aspx
"Question extension" (reference)
If in another version of the. NET environment, you may wish to try
Webclient.proxy = null;
Or
Httpwebrequest.proxy = null;
Add the above sentence Httpwebrequest.proxy = null; when its default proxy is set to empty, HttpWebRequest is not slow at first run, and the response speed is back to normal.