C # HttpWebRequest The first request is slow timeout reason and resolution

Source: Internet
Author: User
Tags http request

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.