我有一个任务,需要像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中实现的答案:如何通过HttpWebRequest发送小写Keep-Alive头部
在HttpClient中是否有类似的方法呢?
client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
并在数据包捕获中得到了 "Connection: Keep-Alive"。 - Maxime EspritConnection: keep-alive,Keep-Alive
。 - foobar