WebRequest objects in C# are useful creations when you want to script interaction with a web server in your application. However, there are some common gotchas that I always have to look up to rectify. So here are three of the most common in one handy location.
Nagle Algorithm
First, remove the use of the Nagle algorithm. The Nagle algorithm is great for protocols like telnet where there is a chance of sending small amounts of data. It is not so good for a protocol where packet sizes are intentionally small. It will kill performance while queuing up bytes of data to send. Use the following:
System.Net.ServicePointManager.UseNagleAlgorithm = false;
Expect 100 Continue
Expect 100 Continue is an HTTP 1.1 addition where a request can detect the readiness of a server before sending the a large body in a post. The WebRequest object always sends an Expect: 100-continue in the header. Not all web servers support handling this (i.e. lighttpd). I suppose there is value to the 100 status code when posting large bodies but for most data transfers (i.e. SOAP, REST, XMLRPC, etc.), it doesn’t seem to be very useful. Use the following to disable this.
System.Net.ServicePointManager.Expect100Continue = false;
WebRequest Proxy
By default, Windows will use the proxy settings internally set. If you know your network is local, allowing the .NET framework to evaluate the default proxy settings can take unnecessary time. You can set .NET to not use or look for any proxy by setting the following code:
WebRequest request = WebRequest.Create (resource); request.Proxy = null;
or
WebRequest.DefaultWebProxy = null;
Please remember to flush.