[英]c# HttpClient connection keepalive
我有一個演示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");
}
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()方法,有人可以告訴我如何實現它嗎? 謝謝
request.KeepAlive = true;
如果我沒記錯的話,我想發表評論,但我沒有代表,很抱歉將其發布為答案,如果有人可以對此建議發表評論,我很樂意刪除我的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.