I got response.StatusCode as 'forbidden'

Nov 8 2019 5:13 AM
Hi Experts,
I have question on how to access web API from WPF. I am very new in this and just figure things out by google topics that I think related to what I need. Basically I need to create WPF application using c# showing list of cafe thats taken from server-side API(documented) and need to use ping to decide which cafe is the nearest.
If you have any resources that can help me with API and ping topics its much appreciated. I have 2 days to figure out everything.
I have yet to figure out the ping part, I just want to retrieve data to WPF from the API but I got status error 'forbiden'. I use below code:
System.Net.ServerPointManager.ServerCertificateValidationCallback = delegate { return true; };
HttpClient client = new HttpClient { 
 BaseAddress = new Uri("httpsxxx.apiary.io/")
} ;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("locations").Result;
if (response.IsSuccessStatusCode) {
var location1 = response.Content.ReadAsAsync<IEnumerable<>>().Result;
grdLocation.ItemSource = location1; 
 else {
MessageBox.Show("Error happen" + response.StatusCode); 
Please help. Thank very much in advance! 

Answers (3)