我创建了一个 HttpClient 来等待服务。这在 asyncron 函数的线程中运行。问题是,服务会在 2 到 3 分钟后发送响应。目前,在我从服务器获得成功响应之前,这些任务将被取消。是否有可能等待超过 2 或 3 分钟的响应?在下面我的代码中:
private async Task<string> loginService()
{
try
{
string post_data = "user_data";
string uri = "http://myserver.de";
HttpClient httpClient = new HttpClient();
httpClient.MaxResponseContentBufferSize = 256000;
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; WIndows NT 6.2; WOW64; Trident/6.0)");
HttpContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("data", post_data)
});
HttpResponseMessage response = await httpClient.PostAsync(new Uri(uri), content);
response.EnsureSuccessStatusCode();
string responseBodyAsText = await response.Content.ReadAsStringAsync();
return responseBodyAsText;
}
catch
{
return "Error...";
}
}
哪种方法最好,我可以在 3 分钟后得到回复?