Make httpheader Connection: Keep-Alive into lower-case “keep-alive”

馋奶兔 提交于 2019-12-06 10:11:45

问题


What I tried it to add new header:

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

Actually it adds keep-alive header into Connection but it doesn't replace old one. So I get Connection: Keep-Alive,keep-alive.

I tried experimenting with Reflection but didn't got anything working.

There is other similar questions about this but there was no solution.


回答1:


Just do the following:

request.Headers.Remove("Connection");
request.Headers.Add("Connection", "keep-alive");

It's not neccessary to set these headers via reflection. In the first place it's important to remove the old entry as an call to Add adds another value if the key already exists (the result you saw with comma separated values).

It'd be even better to use the HttpRequestHeader Enumeration instead of the header name as string:

request.Headers.Remove(HttpRequestHeader.Connection);
request.Headers.Add(HttpRequestHeader.Connection, "keep-alive");

Edit:

My bad. There's an explicit Connection property on the request-object which must be used in that case:

request.Connection = "keep-alive";

FYI: There are some more headers that must be set via their explicit propertries. For a list refer to this page, section remarks: https://msdn.microsoft.com/en-us/library/System.Net.HttpWebRequest%28v=vs.110%29.aspx

Edit2:

Well, looking at the connection property's source code, you can see that it restricts setting these values:

bool fKeepAlive = text.IndexOf("keep-alive") != -1;
bool fClose = text.IndexOf("close") != -1;
if (fKeepAlive || fClose)
{
    throw new ArgumentException(SR.GetString("net_connarg"), "value");
}

So you have 2 options:

  1. Stick with the upper-case value (which I'd prefer) as anyway you have no real reason for it being lower-case ("So I want to have headers exactly as my for example firefox browser."). And as Darin Dimitrov already stated, headers shouldn't be case-sensitive anyway.
  2. Extend your reflection-approach in that way, that you first remove the header an then set it again in lower-case.


来源:https://stackoverflow.com/questions/28250722/make-httpheader-connection-keep-alive-into-lower-case-keep-alive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!