c# – 如何通过HttpClient以小写形式设置“Connection:keep-alive”标题?
作者:互联网
我有一个任务,我需要能够以与Firefox浏览器相同的方式发送Connection:keep-alive标头(请注意,keep-alive必须全部为小写):
"Connection: keep-alive"
但是,我没有运气使用HttpClient实现它.无论我尝试什么,请求总是有
"Connection: Keep-Alive"
这是一个示例代码:
var client = new HttpClient();
var request = new HttpRequestMessage()
{
RequestUri = new Uri("http://www.someURI.com"),
Method = HttpMethod.Get,
};
request.Headers.Connection.Clear(); // No need to do it as it is empty anyway
request.Headers.Connection.Add("keep-alive"); // Still results in "Keep-Alive"
var task = client.SendAsync(request);
另一种尝试:
var client = new HttpClient();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Connection.Add("keep-alive"); // Still results in "Keep-Alive"
string result = await client.GetStringAsync("http://www.someURI.com");
关于如何在HttpWebRequest:How to send lower-case Keep-Alive header through HttpWebRequest中完成它的答案
在HttpClient中可以有类似的东西吗?
解决方法:
只有在Windows平台上运行代码时才会出现此问题.它存在于.Net和.Net Core 2.0中.其根本原因在于win32库“winhttp.dll”,它包含“Keep-Alive”的硬编码字符串,所以如果你需要在windows上运行它,你需要对这个dll做一些事情.
好消息是这个问题不会影响运行Mono或.NET Core 2.0的Linux平台
标签:c,httpclient,keep-alive 来源: https://codeday.me/bug/20190623/1269200.html