c#HttpClient连接keepalive

时间:2015-07-23 15:08:25

标签: c# windows-phone-7 httpclient keep-alive ashx

我有这样的Demo ashx webservice:

    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");
    }

其中trykeepalive就是这样:

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

这只是一个演示,但我有同样的问题,在另一个真正的方法中保持连接活着。

来自手机我通过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();
    }

代码有效,我唯一的问题是始终保持连接存活。 谁能告诉我怎么做?我读了关于方法keepAlive(),有人可以告诉我如何实现它吗?感谢

1 个答案:

答案 0 :(得分:0)

request.KeepAlive = true;如果我没有弄错,我想发表评论,但我缺乏代表,很抱歉将其作为答案发布,如果有人可以评论此建议,我会很乐意删除我的答案。