Is it possible to change headers order using HttpWebRequest?

I need to change the order of headers, I’m using this:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = context.Request.HttpMethod;
request.UserAgent = context.Request.UserAgent;

The output for that is:

GET /* HTTP/1.1
User-Agent: My Server

But it should be

GET /* HTTP/1.1
User-Agent: My Server

Any ideas?

Thanks for your time.

Maybe there’s a way using other object … it’s also an option

There was an outstanding complaint that .NET doesn’t let you modify the Host header a while back. It might not have been resolved. If it is really that important, you could always write socket-level code to send a prepared request (since it’s just text).

I had this problem today but I created this hack:

        /// <summary>
        /// We aren't kids microsoft, we shouldn't need this
        /// </summary>
        public static void UnlockHeaders()
            var tHashtable = typeof(WebHeaderCollection).Assembly.GetType("System.Net.HeaderInfoTable")
                            .GetFields(BindingFlags.NonPublic | BindingFlags.Static)
                            .Where(x => x.FieldType.Name == "Hashtable").Single();

            var Table = (Hashtable)tHashtable.GetValue(null);
            foreach (var Key in Table.Keys.Cast<string>().ToArray())
                var HeaderInfo = Table[Key];
                HeaderInfo.GetType().GetField("IsRequestRestricted", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(HeaderInfo, false);
                HeaderInfo.GetType().GetField("IsResponseRestricted", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(HeaderInfo, false);
                Table[Key] = HeaderInfo;

            tHashtable.SetValue(null, Table);

Then You need call this UnlockHeaders function only one time in the program startup, after call the Header Collection in the HttpWebRequest class will accept any header to be manually added.

Then before add any header to the request, do this:
myHttpWebRequest.Headers["Host"] = "";

After that first header will be the Host, since looks like in some .net versions the Headers field have more priority.

Leave a Comment