我有一个任务,我需要能够以与 Firefox 浏览器相同的方式发送 Connection: keep-alive header (注意 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 中可能会有类似的事情吗?

最佳答案

你试过这个吗:

var client = new HttpClient();
client.DefaultRequestHeaders.Add("Connection", "keep-alive");

它给出了这个:

c# - 如何通过 HttpClient 以小写形式设置  "Connection: keep-alive" header ?-LMLPHP

关于c# - 如何通过 HttpClient 以小写形式设置 "Connection: keep-alive" header ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37005125/

10-13 06:23