Wednesday, April 3, 2013

How to set Connection: Keep-Alive in C# HttpWebRequest class ?

Cannot set by: webrequest.KeepAlive = true;
Have to set this request header by reflection
var req = (HttpWebRequest)WebRequest.Create(someUrl);

var sp = req.ServicePoint;
var prop = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
prop.SetValue(sp, (byte)0, null);

Reference:
http://stackoverflow.com/questions/7458556/c-sharp-connection-keep-alive-header-is-not-being-sent-during-httpwebrequest