0

I am building an application with C# and one of the requirements is that the headers have to contain very specific data. I have been working with the WebClient and HttpWebRequest classes in C# but both do not have the option to explicitly remove the Connection header. This is what I have tried:

WebClient

client.Headers.Remove("Connection");
client.Headers.Add("Connection", null); // if only...

HttpWebRequest

request.Connection = null;
request.Connection = "";

Some of what I have read is that you can only have this header set to Keep-Alive or Close. I need it to not even be on the web request.

All other required headers are adding just fine. It is this one that is causing a head ache. I'm beginning to believe that removing the Connection header is not possible. Is there any language where it is possible if this is the case?

Much appreciation for the time you guys take to look at this!

Thank you!

Zach Yarid
  • 55
  • 5
  • If using an older HTTP protocol version is an option, then check out this answer: https://stackoverflow.com/questions/8476296/removal-of-http-webrequest-elements – Szabolcs Dézsi Sep 29 '17 at 01:42
  • This is the route that I took. It ended up causing a few more grey hairs, but I got exactly what I wanted! Thank you! – Zach Yarid Sep 29 '17 at 20:52

1 Answers1

-1

I'm not quite sure how to remove that specific header, but you could try using the HttpClient class instead of HttpWebRequest or WebClient.

HttpClient class

This class is more up-to-date way of executing HTTP requests in C#

Luke T Brooks
  • 545
  • 1
  • 8
  • 25
  • Awesome. I'll give this a go and report back! Thanks! – Zach Yarid Sep 29 '17 at 01:40
  • The only downside that I see with this HttpClient class is how you have to use objects when POSTing. I would must rather prefer to use a List or a NameValueCollection object, if possible. – Zach Yarid Sep 29 '17 at 01:55
  • You can post Lists using the `PostAsJsonAsync` method [https://msdn.microsoft.com/en-us/library/hh944521(v=vs.118).aspx] – Luke T Brooks Sep 29 '17 at 02:00