Why is this WebRequest code slow?

I noticed that an HttpWebRequest hangs on the first request. I did some research and what seems to be happening is that the request is configuring or auto-detecting proxies. If you set

request.Proxy = null;

on the web request object, you might be able to avoid an initial delay.

With proxy auto-detect:

using (var response = (HttpWebResponse)request.GetResponse()) //6,956 ms
{
}

Without proxy auto-detect:

request.Proxy = null;
using (var response = (HttpWebResponse)request.GetResponse()) //154 ms
{
}

Leave a Comment