Matapolo
Matapolo

Reputation: 667

HttpClient / in a metro app

I have create an HttpClient to wait for a service. This runs in a thread in a asyncron function. The problem is, that the service send a response after 2 - 3 minutes. At the moment the tasks will be cancelled befor I get a successfull response from the server. Is there a possibility to wait for a response over 2 or 3 minutes? In the following my code:

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

Which method is the best, which I can use to get a response after 3 minutes?

Upvotes: 3

Views: 997

Answers (1)

John Koerner
John Koerner

Reputation: 38077

The default value for the timeout is 100 seconds. You can up the timeout on the HTTPClient to 3 minutes(or higher) using the Timeout property.

 httpClient.Timeout = new TimeSpan(0,3,0);

Upvotes: 1

Related Questions