Search code examples
c#httpkeep-alive

How to send lower-case Keep-Alive header through HttpWebRequest


I'm writing a bot, which should emulate firefox as closely as possible. By examining the headers that it is sending, I've found one minor difference, that I do not know how to get rid off:

Firefox uses following keep-alive header:

Connection: keep-alive

While c# always sends out:

Connection: Keep-Alive

I know that it probably does not matter, but I'd still love to know if there is any way/hack to modify that header to be all lower case.

Any ideas how to do this?


Solution

  • In .net 4.0 this works:

    request.Headers.GetType().InvokeMember(
        "ChangeInternal",
        BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
        Type.DefaultBinder, 
        request.Headers, 
        new object[] { "Connection", "keep-alive" }
    );
    

    Just be sure you don't actually set the KeepAlive property on the request