Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

System.Net.Http.HttpClient Timeout seems to be ignored

$
0
0

I am using Xamarin.iOS Version: 8.10.5.26 (Indie Edition) and facing a very strange behaviour with timing out requests sent with HttpClient():

The following code tries to get a result from an url, and has 60 seconds timeout (1 minutes), but when the request is fired it is taking around 90 seconds for time out. When call is made, I manually turn off the network connectivity to check the time out. It is observed that it is taking more than 60 seconds.

CODE

`public async Task<Dictionary<string,object>> GetPatientDataASync (string lUsername)
{
var lDict = new Dictionary<string,object> ();
try {
string lQuerystring = "{Email: '" + lUsername + "'}";
String lUrl = String.Format (Constants.mURLPatient + "?where={0}", JObject.Parse (lQuerystring));
var lClient = new HttpClient ();
lClient.BaseAddress = new Uri (lUrl);
lClient.DefaultRequestHeaders
.Accept
.Add (new MediaTypeWithQualityHeaderValue ("application/json"));
lClient.DefaultRequestHeaders.Add ("X-Parse-Application-Id", Constants.mKeyParseAppId);
lClient.DefaultRequestHeaders.Add ("X-Parse-REST-API-Key", Constants.mKeyRestAPIKey);
lClient.Timeout = new TimeSpan (0, 1, 0);
var request = new HttpRequestMessage ();
request.Method = HttpMethod.Get;
if (Utility.isNetworkConnected ()) {
bool responseStatus = false;
await lClient.SendAsync (request)
.ContinueWith (responseTask => {
if (responseTask != null) {
var response = responseTask.Result;
if (response != null) {
if (response.IsSuccessStatusCode) {
var responseContent = response.Content;
if (responseContent != null) {
string responseString = responseContent.ReadAsStringAsync ().Result;
if (!string.IsNullOrWhiteSpace (responseString)) {
JObject json = JObject.Parse (responseString);
if (json != null) {
if (json ["results"].Any ()) {
Patient user = Patient.Instance;
user.objectId = json.SelectToken (@results[0].objectId).Value ();
user.Email = json.SelectToken (@results[0].Email).Value ();
user.Name = json.SelectToken (@results[0].Name).Value ();
user.IsNotificationsEnabled = json.SelectToken (@results[0].IsNotificationsEnabled).Value ();

                                                Application.Current.Properties ["IsNotificationsEnabled"] = json.SelectToken (@"results[0].IsNotificationsEnabled").Value<string> ();

                                            if (json.SelectToken (@"results[0].DeviceToken") != null) {
                                                var deviceToken = json.SelectToken (@"results[0].DeviceToken").Value<JArray> ();
                                                if (deviceToken != null)
                                                    user.DeviceToken = deviceToken.ToObject < List<string>> ();
                                            } else {
                                                user.DeviceToken = new List<string> ();
                                            }

                                            var doctors = json.SelectToken (@"results[0].MyDoctors").Value<JArray> ();
                                            user.AllergicTo = json.SelectToken (@"results[0].AllergicTo").Value<string> ();
                                            user.ContactNo = json.SelectToken (@"results[0].ContactNo").Value<string> ();
                                            user.BloodGroup = json.SelectToken (@"results[0].BloodGroup").Value<string> ();
                                            user.MyDoctors = doctors != null ? doctors.ToObject<List<string>> () : new List<string> ();
                                            responseStatus = true;
                                        } else
                                            responseStatus = false;
                                    }
                                }
                            }
                        }
                    }
                }
            });
          lDict.Add (SUCCESS_CODE, responseStatus);
            return lDict;
        } else {
            lDict.Add (NO_INTERNET, Constants.mStringNoInternetMessage);
            return lDict;
        }
    } catch (Exception e) {
        Debug.WriteLine (e.Message + "\n " + e.StackTrace);
        lDict.Add (EXCEPTION_OCCURED, e);
        return lDict;
    }
}`

If there's some mistake in my code , please do let me know.


Viewing all articles
Browse latest Browse all 77050

Trending Articles