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
Model:
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 = "http://192.168.0.185/swiftEntries.Api/api/";
public async Task<string> PostVisitorsInAsync(string Name, string Place, string Organisation, string PersonOfInterest,
string PersonOfMeet)
{
try
{
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.
Debug.WriteLine(content);
return content;
}
catch (Exception e)
{
Console.WriteLine("Exception Occurss here "+e.StackTrace);
}
return null;
}
Login 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.