简体   繁体   English

C#如何将HttpClient Keep-Alive设置为false

[英]C# How to set HttpClient Keep-Alive to false

I had a low performance problem with HTTP requests on .NET. 我在.NET上的HTTP请求有一个低性能问题。 The HTTP GET request to a REST API on the localhost took about 500 ms to complete. 对localhost上的REST API的HTTP GET请求大约需要500毫秒才能完成。 I spent a lot of time to fix it. 我花了很多时间来解决它。 I have tried many ways: HttpClient , HttpWebRequest , WebClient and RestSharp . 我尝试了很多方法: HttpClientHttpWebRequestWebClientRestSharp None of them work. 他们都没有工作。 Most solutions on the Internet said to set Proxy parameter to null but it still won't work faster. 互联网上的大多数解决方案都说将Proxy参数设置为null但它仍然无法更快地运行。

The only way I found to reduce this time is to set the Keep-Alive parameter of request to false: 我发现减少这个时间的唯一方法是将请求的Keep-Alive参数设置为false:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.KeepAlive = false;

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This works great. 这非常有效。 Time is reduced to 7-10 ms. 时间减少到7-10毫秒。 But now in some reasons I need to use HttpClient instead of HttpWebRequest . 但现在由于某些原因我需要使用HttpClient而不是HttpWebRequest And I can't find how to set Keep-Alive to false for HttpClient . 而且我找不到如何为HttpClient设置Keep-Alive为false。 The only thing I found is how to set it to true by setting a "connection" header to "Keep-Alive". 我发现的唯一一件事就是如何通过将“连接”标题设置为“保持活跃”来将其设置为true。

I am using this code for POST request by HttpClient: 我在HttpClient上使用此代码进行POST请求:

        HttpClient _http = new HttpClient();
        _http.DefaultRequestHeaders.Accept.Clear();
        _http.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
        _http.DefaultRequestHeaders.Add("Keep-Alive", "timeout=600");

        var content = new StringContent(
            request, Encoding.UTF8, "application/%appname%+xml");
        content.Headers.ContentType.Parameters.Add(
            new NameValueHeaderValue("type", "payload"));

        HttpResponseMessage response = await _http.PostAsync(uri, content);

And it still takes about 500-600 ms to complete. 它仍然需要大约500-600毫秒才能完成。

When you set HttpWebRequest.KeepAlive = true the header set is Connection: keep-alive 当您设置HttpWebRequest.KeepAlive = true ,标头集是Connection:keep-alive

When you set HttpWebRequest.KeepAlive = false the header set is Connection: close 设置HttpWebRequest.KeepAlive = false ,标头集为Connection:close

So you will need 所以你需要

_http.DefaultRequestHeaders.Add("Connection", "close");

Use this code to disable HTTP Keep-Alive on the client: 使用此代码禁用客户端上的HTTP Keep-Alive:

_http.DefaultRequestHeaders.ConnectionClose = true;

This will set Connection request header to close . 这会将Connection请求标头设置为close

See code below: 见下面的代码:

HttpClient cli;
...
cli.DefaultRequestHeaders.Add("Connection", "keep-alive");
cli.DefaultRequestHeaders.Add("Keep-Alive", "600");

我希望这可以帮助你,我已经测试过了

_client.DefaultRequestHeaders.Connection.Add("Keep-Alive");

I wrote HttpClientHandler, and in the handler just before the request I deleted the header "Connection". 我写了HttpClientHandler,并在请求之前的处理程序中删除了标题“Connection”。 In this case, it sets the default "Connection: keep-alive". 在这种情况下,它设置默认的“Connection:keep-alive”。 Even if before that you put it in the HttpClient by default, set properties, etc. Check your handlers. 即使在此之前,您默认将它放在HttpClient中,设置属性等。检查您的处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM