PageRenderTime 110ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/WifiChannelSpread/WiFiChannelSpread/GoogleApi/GoogleApi.cs

#
C# | 90 lines | 76 code | 14 blank | 0 comment | 4 complexity | c1e52cd2f1bdc23508369c43f5251afe MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Collections.ObjectModel;
  8. namespace WiFiChannelSpread.GoogleApi
  9. {
  10. public static class GeolocationApi
  11. {
  12. private const int HttpRequestTimeoutMilliseconds = 5000;
  13. private const string GeolocationApiUrl = "http://www.google.com/loc/json";
  14. private const string ApplicationHost = "WiFiChannelSpread.CodePlex.com";
  15. private const string GeolocationApiVersion = "1.1.0";
  16. public static Location GetGeolocation(Collection<WiFiTower> listOfWiFiTowers, bool includeAddress)
  17. {
  18. GeolocationRequest request = CreateGeolocationRequest(includeAddress);
  19. request.wifi_towers = listOfWiFiTowers;
  20. string jsonResponse = PostHttpJson(GeolocationApiUrl, Newtonsoft.Json.JsonConvert.SerializeObject(request));
  21. GeolocationResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<GeolocationResponse>(jsonResponse);
  22. if (response == null)
  23. {
  24. throw new InvalidOperationException("No response from Google.");
  25. }
  26. return response.location;
  27. }
  28. public static Location GetGeolocationOfWiFiTower(string macAddress, string ssid, bool includeAddress)
  29. {
  30. GeolocationRequest request = CreateGeolocationRequest(includeAddress);
  31. request.wifi_towers.Add(new WiFiTower { mac_address = macAddress, ssid = ssid });
  32. string jsonResponse = PostHttpJson(GeolocationApiUrl, Newtonsoft.Json.JsonConvert.SerializeObject(request));
  33. GeolocationResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<GeolocationResponse>(jsonResponse);
  34. if (response == null)
  35. {
  36. throw new InvalidOperationException("No response from Google.");
  37. }
  38. return response.location;
  39. }
  40. private static GeolocationRequest CreateGeolocationRequest(bool includeAddress)
  41. {
  42. return new GeolocationRequest
  43. {
  44. host = ApplicationHost,
  45. version = GeolocationApiVersion,
  46. request_address = includeAddress,
  47. wifi_towers = new Collection<WiFiTower>()
  48. };
  49. }
  50. private static string PostHttpJson(string url, string data)
  51. {
  52. byte[] postData = Encoding.ASCII.GetBytes(data);
  53. HttpWebRequest request = CreateWebRequest(url);
  54. request.Method = "POST";
  55. request.ContentType = "application/json; charset=utf-8";
  56. request.Accept = "application/json, text/javascript, */*";
  57. request.ContentLength = postData.Length;
  58. using (Stream requestStream = request.GetRequestStream())
  59. {
  60. requestStream.Write(postData, 0, postData.Length);
  61. }
  62. using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  63. using (Stream responseStream = response.GetResponseStream())
  64. using (StreamReader reader = new StreamReader(responseStream))
  65. {
  66. return reader.ReadToEnd();
  67. }
  68. }
  69. private static HttpWebRequest CreateWebRequest(string url)
  70. {
  71. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  72. request.Timeout = HttpRequestTimeoutMilliseconds;
  73. return request;
  74. }
  75. }
  76. }