How to change the “Connection: Keep-Alive” header to lowercase “keep-alive” in C#


How to change the “Connection: Keep-Alive” header to lowercase “keep-alive” in C#



I am sending some requests to a host which keeps rejecting my requests because of the Connection header in my requests. I need to change it to lowercase. I have the exact same issue as described in this question. However, that question is 3 years old, so I was wondering if there is a solution to this problem now?


Connection



The answer there talks about using reflection to remove the header first before setting it again in lower-case. Can someone explain how do I achieve that? Thanks.





docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/…
– Simon Price
Jun 30 at 20:21





@user2864740 No. "Some common headers are considered restricted and are either exposed directly by the API (such as Content-Type) or protected by the system and cannot be changed." Unfortunately, Connection is one such header.
– Anon
Jun 30 at 20:49


Connection




1 Answer
1



Stop and read this solution using reflection on ChangeInternal, which is probably a 'sufficient' level of reflection. When using it, do not set Connection via the property.


ChangeInternal



"And let's never speak of this again.."


private static void SetHeaderRestriction(string name, bool restricted) {
var hInfoPi = typeof(WebHeaderCollection)
.GetField("HInfo", BindingFlags.NonPublic | BindingFlags.Static);
var headerInfoTableType = hInfoPi.GetValue(null).GetType();
var headerInfoHashPi = headerInfoTableType
.GetField("HeaderHashTable", BindingFlags.NonPublic | BindingFlags.Static);
// Internal cache singleton of header info / restriction data
var headerInfoHash = (Hashtable)headerInfoHashPi.GetValue(null);
var connectionHeaderInfo = headerInfoHash["Connection"];
// IsRequestRestricted is 'readonly', but reflection can trump.
// An alternative would be to [temporarily] replace the entry entirely.
var restrictedPi = connectionHeaderInfo.GetType()
.GetField("IsRequestRestricted", BindingFlags.NonPublic | BindingFlags.Instance);
restrictedPi.SetValue(connectionHeaderInfo, restricted);
}

void Main()
{
var wr = (HttpWebRequest)WebRequest.Create("http://www.google.com");

SetHeaderRestriction("Connection", false);
wr.Headers["Connection"] = "keep-alive";
SetHeaderRestriction("Connection", true);

wr.Connection.Dump(); // "keep-alive"
((HttpWebResponse)wr.GetResponse()).StatusCode.Dump(); // OK
}



This code executes in LINQPad (which is where Dump() comes from) and .NET 4.7.1. YMMV.


Dump()





I am using HTTPWebRequest class and setting the headers using the Headers property. With your code, I see "Connection: keep-alive, Keep-Alive" in Fiddler, despite not using the Connection property anywhere in my code.
– Anon
Jul 1 at 7:40





Interesting. There must be something in the bowls of generating the request later sigh.
– user2864740
Jul 1 at 9:07





@Anon What happens if setting wr.KeepAlive = false before assigning the header value? The default of the property is true. The "Keep-Alive" part of the header is coming in later, but should not be added if 'disabled' via the property.
– user2864740
Jul 1 at 9:09



wr.KeepAlive = false





If I set request.KeepAlive = false before assigning the header value, I get Connection: keep-alive, Close in the request header.
– Anon
Jul 1 at 9:21



request.KeepAlive = false


Connection: keep-alive, Close





@Anon Ahh, per referencesource.microsoft.com/#system/net/system/Net/… KeepAlive must be false and IsVersionHttp10 must be true or else "Keep-Alive" and/or "Close" will be added when preparing the request headers. However, that will also use "1.0" in the request elsewhere yuck!!! - what about using HttpClient? Is that more 'amendable'?
– user2864740
Jul 1 at 19:11



KeepAlive


IsVersionHttp10






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter