简体   繁体   English

C#HttpClient连接保持连接

[英]c# HttpClient connection keepalive

i have a Demo ashx webservice like this: 我有一个演示ashx web服务,像这样:

    public void ProcessRequest(HttpContext context)
    {
        //string s = context.Request.Form["prova"];
        string a= context.Request.Form["a"];
        string b= context.Request.Form["b"];

        TryKeepAlive();
        context.Response.Write("ok");
    }

where trykeepalive just does: trykeepalive只是在哪里:

    private void TryKeepAlive()
    {
        int msTimeout = 999999999;
        System.Threading.Thread.Sleep(msTimeout);
    }

this is just a demo, but i have the same problem of keeping connection alive in another real method. 这只是一个演示,但是我在另一个实际方法中也存在保持连接存活的相同问题。

from phone i'm calling the post via HttpClient: 从电话中,我通过HttpClient致电该帖子:

    public async void PostIt()
    {
        string url = "myurl.htm";
        var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("a", "12345"),
                    new KeyValuePair<string, string>("b", "123456")
                };
        var httpClient = new HttpClient(new HttpClientHandler());

        HttpResponseMessage response = await httpClient.PostAsync(url, new FormUrlEncodedContent(values));
        response.EnsureSuccessStatusCode();
        var responseString = await response.Content.ReadAsStringAsync();
    }

the code works, my only problem is keeping connection alive for all time. 代码有效,我唯一的问题是始终保持连接状态。 can someone tell me how to do it? 有人可以告诉我该怎么做吗? i read about the method keepAlive(), can someone show me how to implement it? 我了解了keepAlive()方法,有人可以告诉我如何实现它吗? thanks 谢谢

request.KeepAlive = true; If I'm not mistaken, I wanted to comment but I lack rep, sorry for posting it as an answer, if someone can comment this suggestion I'll gladly delete my answer. 如果我没记错的话,我想发表评论,但我没有代表,很抱歉将其发布为答案,如果有人可以对此建议发表评论,我很乐意删除我的答案。

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

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