WebApiHelp.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Ant.Service.Common
  10. {
  11. public class WebApiHelp
  12. {
  13. /// <summary>
  14. /// http://apilocate.amap.com/position?accesstype=0&imei=111111111111515&smac=E0:DB:55:E4:C7:49&cdma=0
  15. /// &bts=460,1,17431,1102,-13&nearbts=460,1,17431,1102,-13
  16. /// &key=faeed74bf5b723240d2bc8b83add4311&network=GPRS
  17. /// </summary>
  18. /// <returns></returns>
  19. public static GPSRoot getLocationGPS(string imei, int mcc, int mnc, int lac, int ci, int rxlev)
  20. {
  21. try
  22. {
  23. string key = "faeed74bf5b723240d2bc8b83add4311";
  24. Dictionary<string, string> parames = new Dictionary<string, string>();
  25. parames.Add("accesstype", "0");
  26. parames.Add("imei", imei);
  27. parames.Add("cdma", "0");
  28. parames.Add("network", "GPRS");
  29. string btsstr = mcc + "," + mnc + "," + lac + "," + ci + "," + rxlev;
  30. parames.Add("bts", btsstr);
  31. parames.Add("nearbts", btsstr + "|" + btsstr + "|" + btsstr);
  32. parames.Add("key", key);
  33. Tuple<string, string> parameters = GetQueryString(parames);
  34. var carstate = Get<GPSRoot>("http://apilocate.amap.com/position", parameters.Item2);
  35. return carstate;
  36. }
  37. catch (Exception ex)
  38. {
  39. GPSRoot request = new GPSRoot();
  40. request.status = "error";
  41. // LoggerHelper.Error("getLocationGPS出现错误", ex);
  42. return request;
  43. }
  44. }
  45. /// <summary>
  46. /// 用户需要登录Get请求
  47. /// </summary>
  48. /// <typeparam name="T"></typeparam>
  49. /// <param name="webApi"></param>
  50. /// <param name="queryStr"></param>
  51. /// <param name="staffId"></param>
  52. /// <returns></returns>
  53. public static T Get<T>(string webApi, string queryStr)
  54. {
  55. try
  56. {
  57. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webApi + "?" + queryStr);
  58. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  59. Stream streamReceive = response.GetResponseStream();
  60. StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
  61. string strResult = streamReader.ReadToEnd();
  62. streamReader.Close();
  63. streamReceive.Close();
  64. request.Abort();
  65. response.Close();
  66. return JsonConvert.DeserializeObject<T>(strResult);
  67. }
  68. catch (Exception ex)
  69. {
  70. return default(T);
  71. }
  72. }
  73. /// <summary>
  74. /// 拼接get参数
  75. /// </summary>
  76. /// <param name="parames"></param>
  77. /// <returns></returns>
  78. public static Tuple<string, string> GetQueryString(Dictionary<string, string> parames)
  79. {
  80. // 第一步:把字典按Key的字母顺序排序
  81. IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parames);
  82. IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
  83. // 第二步:把所有参数名和参数值串在一起
  84. StringBuilder query = new StringBuilder(""); //签名字符串
  85. StringBuilder queryStr = new StringBuilder(""); //url参数
  86. if (parames == null || parames.Count == 0)
  87. return new Tuple<string, string>("", "");
  88. while (dem.MoveNext())
  89. {
  90. string key = dem.Current.Key;
  91. string value = dem.Current.Value;
  92. if (!string.IsNullOrEmpty(key))
  93. {
  94. query.Append(key).Append(value);
  95. queryStr.Append("&").Append(key).Append("=").Append(value);
  96. }
  97. }
  98. return new Tuple<string, string>(query.ToString(), queryStr.ToString().Substring(1, queryStr.Length - 1));
  99. }
  100. }
  101. public class Result
  102. {
  103. /// <summary>
  104. ///
  105. /// </summary>
  106. public string type { get; set; }
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. public string location { get; set; }
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. public string radius { get; set; }
  115. /// <summary>
  116. /// 江苏省 常州市 新北区 汉江中路 靠近中国建设银行(新北支行)
  117. /// </summary>
  118. public string desc { get; set; }
  119. /// <summary>
  120. /// 中国
  121. /// </summary>
  122. public string country { get; set; }
  123. /// <summary>
  124. /// 江苏省
  125. /// </summary>
  126. public string province { get; set; }
  127. /// <summary>
  128. /// 常州市
  129. /// </summary>
  130. public string city { get; set; }
  131. /// <summary>
  132. ///
  133. /// </summary>
  134. public string citycode { get; set; }
  135. /// <summary>
  136. ///
  137. /// </summary>
  138. public string adcode { get; set; }
  139. /// <summary>
  140. /// 汉江中路
  141. /// </summary>
  142. public string road { get; set; }
  143. /// <summary>
  144. /// 汉江中路
  145. /// </summary>
  146. public string street { get; set; }
  147. /// <summary>
  148. /// 中国建设银行(新北支行)
  149. /// </summary>
  150. public string poi { get; set; }
  151. }
  152. public class GPSRoot
  153. {
  154. /// <summary>
  155. ///
  156. /// </summary>
  157. public string status { get; set; }
  158. /// <summary>
  159. ///
  160. /// </summary>
  161. public string info { get; set; }
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. public string infocode { get; set; }
  166. /// <summary>
  167. ///
  168. /// </summary>
  169. public Result result { get; set; }
  170. }
  171. }