C # the first time I use GetRequestStream (), it takes 20 seconds

I am using C #.

The first time I use WebRequest GetRequestStream () in my code, it takes up to 20 seconds. After that, it will take less than 1 second.

Below is my code. The string "this.requestStream = httpRequest.GetRequestStream ()" causes a delay.

StringBuilder postData = new StringBuilder(100); postData.Append("param="); postData.Append("test"); byte[] dataArray = Encoding.UTF8.GetBytes(postData.ToString()); this.httpRequest = (HttpWebRequest)WebRequest.Create("http://myurl.com"); httpRequest.Method = "POST"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.ContentLength = dataArray.Length; this.requestStream = httpRequest.GetRequestStream(); using (requestStream) requestStream.Write(dataArray, 0, dataArray.Length); this.webResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8); String responseString = responseReader.ReadToEnd(); 

How can I find out what causes this? (ex: DNS lookup? Server not responding?)

Thanks and respect, Koen

+7
c #
source share
4 answers

You can also try setting .Proxy = null. Sometimes it tries to auto-detect a proxy server that takes time.

+11
source share

It looks like your application is precompiled on first click. This is how .net works.

Here's a way to speed up your web application. link text

+1
source share

This is actually a framework for HTML operations that perform proxy checks at startup to set the HttpWebRequest.DefaultWebProxy property.

In my application, as part of the launch actions, I create a fully formed request as the main task to get this overhead.

 HttpWebRequest web = (HttpWebRequest)WebRequest.Create(m_ServletURL); web.UserAgent = "Mozilla/4.0 (Windows 7 6.1) Java/1.6.0_26"; 

Setting the UserAgent field in my case launches startup overhead.

+1
source share

One problem might be that .NET only allows two connections at a time by default .

You can increase the number of simultaneous connections with:

  ServicePointManager.DefaultConnectionLimit = newConnectionLimit; 

We leave the determination of the optimal value for the user.

0
source share

All Articles