Forum Xamarin.Forms

Receiving a connection Timed Out every time I try sending data to server

russferns22russferns22 Member ✭✭
edited September 2018 in Xamarin.Forms

I'm receiving a connection Timed out /Connection Failure error whenever my break point hits the content class which attempts to send my http post request to my server ,In my case I'm using .Net framework.
My service class receives the model but cannot post it to the URL.
I'm following an MVVM architecture.

Below are the code snippets


 public  class VisitorsIn
        public string Name { get; set; }
        public string Place { get; set; }
        public string Organisation { get; set; }
        public string PersonOfInterest { get; set; }
        public string PersonOfMeet { get; set; }


ApiService Class

  public class ApiServices
        string BaseUrl = "";

       public async Task<string> PostVisitorsInAsync(string Name, string Place, string Organisation, string PersonOfInterest,
       string PersonOfMeet)

         ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
         var client = new HttpClient();
         client.DefaultRequestHeaders.ExpectContinue = false;
         var visitor = new VisitorsIn();

            visitor.Name = Name;
            visitor.Place = Place;
            visitor.Organisation = Organisation;
            visitor.PersonOfInterest = PersonOfInterest;
            visitor.PersonOfMeet = PersonOfMeet;

        var json = JsonConvert.SerializeObject(visitor);
        HttpContent c = new StringContent(json);
        c.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            //var request = new HttpRequestMessage(HttpMethod.Post, BaseUrl + "VisitorsDetails/SaveVisitorDetails");
            //client.Timeout = TimeSpan.FromSeconds(60);
        var url = BaseUrl + "VisitorsDetails/SaveVisitorDetails/";
        var response = client.PostAsync(url,c).Result;

            **var content = await response.Content.ReadAsStringAsync(); **   //this is where the code crashes when it comes to sending data.

            return content;

        catch (Exception e)
            Console.WriteLine("Exception Occurss here "+e.StackTrace);
        return null;

View Model

        public class VisitorsInViewModel: INotifyPropertyChanged
            public Action DisplayInvalidLoginPrompt;
            public event PropertyChangedEventHandler PropertyChanged = delegate { };

            ApiServices _apiServices = new ApiServices();

            public string Name { get; set; }
            public string Place { get; set; }
            public string Organisation { get; set; }
            public string PersonOfInterest { get; set; }
            public string PersonOfMeet { get; set; }

           public ICommand VisitorsInSaveCommand   { get; set;  }

            public VisitorsInViewModel()
                VisitorsInSaveCommand = new Command(OnSubmit);

                Name = Name;
                Place = Place;
                Organisation = Organisation;
                PersonOfInterest = PersonOfInterest;
                PersonOfMeet = PersonOfMeet;

            public async void OnSubmit()
               await _apiServices.PostVisitorsInAsync(Name,Place,Organisation,PersonOfInterest,PersonOfMeet);

Things that have been already tested::-
1) Ensured theres no mistake with the apis provided, they've been tested in postman.
2) Ensured with break points that servcie class receives the model.
3) The point at which my code crashes in the service class has been commented above in the service class.
4) The ViewModel and View work accurately, I haven't included my View in this discussion as I;m just binding all the properties there.


  • HarshitaHarshita INMember ✭✭✭✭

    Set this
    client.Timeout=new TimeSpan(0,10,0); // set the timeout so,here I set 10 minute maximum

  • russferns22russferns22 Member ✭✭

    hey @Harshita I resolved the error by modifying my code just a little

     var response = client.PostAsync(url,c).Result;
     var content = await response.Content.ReadAsStringAsync(); 

    After Edit

    HttpResponseMessage response = await client.PostAsync(url, c);
    var content = await response.Content.ReadAsStringAsync();

    and I made sure my Android device i was using was on the same network ,rather then the mobile data.
    This instantly solved my issue and now my data is getting saved to the db.

    Mighty Thanks!.

  • BushbertBushbert Member ✭✭✭

    I wouldn't implement a lengthy timeout with considering cancellation support otherwise your users will be frequently heading for a coffee break.

    The first thing I would ask is your service definitely running on port 80 rather some other typical debugging port?

    Can I suggest your first check the HTTP header before trying to deserialize the content? This will help you determine if its a network connectivity or a service issue i.e.

    if (result.IsSuccessStatusCode)
        content = await response.Content.ReadAsStringAsync();

    You can use a proxy like Fiddler to examine your HTTP traffic to see whats going on which I find saves a lot of time.

Sign In or Register to comment.