Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I use C#.

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

Below is my code. The row "this.requestStream = httpRequest.GetRequestStream()" is causing the 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 see what causes this? (for instance: DNS lookup? Server not responding?)

Thanks and regards, Koen

like image 752
koen Avatar asked Jul 28 '10 15:07

koen


4 Answers

You could also try to set the .Proxy = null. Sometimes it tries to autodetect a proxy which takes up time.

like image 53
Wimpje Avatar answered Nov 15 '22 16:11

Wimpje


That sounds like your application is pre-compiling when you first hit it. This is how .net works.

Here is a way to speed up your web app. link text

like image 29
skyfoot Avatar answered Nov 15 '22 17:11

skyfoot


It's actually the framework for HTML operations doing startup network proxy checking to setup the property HttpWebRequest.DefaultWebProxy.

In my application as part of the startup actions I create a fully formed request as a back ground task to get this overhead out of the way.

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 is triggers the startup overhead.

like image 30
freds Avatar answered Nov 15 '22 16:11

freds


I had the same issue but .proxy = null didn't solve it for me. Depending on the network structure the problem might be connected to IPv6. The first request nearly took exactly 21sec each time the application run. Therefore I argue it must be a timeout value. If this value is reached the fallback solution IPv4 is used (for subsequent calls as well). Forcing the use of IPv4 in the first place solved the issue for me!

HttpWebRequest request = WebRequest.Create("http://myurl.com") as HttpWebRequest;
request.ServicePoint.BindIPEndPointDelegate = (servicePount, remoteEndPoint, retryCount) =>
{
    if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    {
        return new IPEndPoint(IPAddress.Any, 0);
    }
    throw new System.InvalidOperationException("No IPv4 address found.");
};
like image 42
urbanSoft Avatar answered Nov 15 '22 17:11

urbanSoft