I am having trouble retrieve data from a webservice using a PLC project in Xamarin forms. I have tried both the WebRequest and HttpClient but get various exceptions. I have also tried to run the code from this sample:
ConnectFailure (Network is unreachable)
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create ("http://localhost:3000/profile/contacts"); webRequest.UseDefaultCredentials = true; webRequest.Method = "GET"; webRequest.Accept = "application/json"; webRequest.ContentType = "application/json"; webRequest.BeginGetResponse ((ar) => { var request = (HttpWebRequest)ar.AsyncState; try { using (var response = (HttpWebResponse)request.EndGetResponse (ar)) { var s = response.GetResponseStream (); } } catch (Exception ex) { var xy = ex; } }, webRequest);ConnectFailure (The requested address is not valid in this context)
var client = new System.Net.Http.HttpClient (); client.BaseAddress = new Uri("http://localhost:3000/"); client.DefaultRequestHeaders.Clear (); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = await client.GetStringAsync ("profile/contacts");
I have also tried to run the code from this sample: https://github.com/conceptdev/xamarin-forms-samples/tree/master/HttpClient/HttpClientDemo Which throws a namesresolutionfail exception when trying to make a request.
var client = new System.Net.Http.HttpClient ();
client.BaseAddress = new Uri("http://api.geonames.org/");
var response = await client.GetAsync("earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=bertt");
var earthquakesJson = response.Content.ReadAsStringAsync().Result;
var rootobject = JsonConvert.DeserializeObject<Rootobject>(earthquakesJson);
return rootobject.earthquakes;
I have tested my rest api (written in Node.Js) and it is returning data as expected.