How to set the Timeout for request and get the response code for HttpRequest in MvvmCross xamarin

Viewed 9132

Code I have constructed so for:

 public class RestService : IRestService
    {
        public async Task<StellaData> GetStellConfigData()
        {
            try
            {   
                //Declare a Http client
                HttpClient client = new HttpClient();
                //Add a Base URl
                //client.BaseAddress = new Uri(Constants.MUrl);
                //Add the response type
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                //Add the API
                var response =await client.GetStringAsync(new Uri(Constants.mUrl));

                var myItems = Newtonsoft.Json.JsonConvert.DeserializeObject<StellaData>(response);
                return myItems;

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return null;
        }
    }

What I am trying to do:

  1. Set a Timeout for the request
  2. Catch Related Exception for the timeout for the request
  3. Get the response code for the request
2 Answers
Related