When I first make a HttpWebRequest in a Winform App, it takes 10~30 seconds to process the first request. Subsequent calls takes less than a second. I wrote a little POC app to exemplify this, it consists of two buttons and 2 textBoxes, an image of the app can be found here (I can't upload images here yet...):

The app code is very simple:
public partial class Form1 : Form
{
DateTime _start;
TimeSpan _span;
int _count = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
_count++;
_start = DateTime.Now;
HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(textBox1.Text); ;
// Added after Conrad's response:
_request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();
_response.Close();
_span = DateTime.Now - _start;
textBox2.Text += _count.ToString("000") + ": " + _span.ToString(@"mm\:ss\,fff") + "\r\n";
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}
}
Why does it take so long in the 1st request? There is something I can do to speed this up?
If you set the proxy property to null it will work as well, even without disabling the logical cards.
Source: HttpWebRequest runs slowly first time within SQLCLR
Mystery solved, thank you all guys for the insigths. Using Fiddler2 (Many thanks, Bradley & Jason!),I was able to determine the problem was on my notebook. I have one MS-Loopback & one VirtualBox adapter, that I use for tests with PDA's & VM's. When one or both are enabled, the app took too long to determine the gateway for the connection. When I disabled both logical cards, the lag was gone.
I still think there is something weird with the WinForm app/.Net, because my browsers (IE/FF) does not suffer from this problem. Maybe there is a way to tweak the .NET or my app to ignore this adapters, but since I'm a almost complete webnorant (Web-ignorant), I'm happy to have find this solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With