Call a REST Web Service - Xamarin


SUBMITTED BY: inzi

DATE: April 2, 2017, 4:28 p.m.

FORMAT: C#

SIZE: 911 Bytes

HITS: 713

  1. Call a REST Web Service - Xamarin
  2. private async Task<JsonValue> FetchWeatherAsync (string url)
  3. {
  4. // Create an HTTP web request using the URL:
  5. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
  6. request.ContentType = "application/json";
  7. request.Method = "GET";
  8. // Send the request to the server and wait for the response:
  9. using (WebResponse response = await request.GetResponseAsync ())
  10. {
  11. // Get a stream representation of the HTTP web response:
  12. using (Stream stream = response.GetResponseStream ())
  13. {
  14. // Use this stream to build a JSON document object:
  15. JsonValue jsonDoc = await Task.Run (() => JsonObject.Load (stream));
  16. Console.Out.WriteLine("Response: {0}", jsonDoc.ToString ());
  17. // Return the JSON document:
  18. return jsonDoc;
  19. }
  20. }
  21. }

comments powered by Disqus