using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Ant.Service.Common
{
public class WebApiHelp
{
///
/// http://apilocate.amap.com/position?accesstype=0&imei=111111111111515&smac=E0:DB:55:E4:C7:49&cdma=0
/// &bts=460,1,17431,1102,-13&nearbts=460,1,17431,1102,-13
/// &key=faeed74bf5b723240d2bc8b83add4311&network=GPRS
///
///
public static GPSRoot getLocationGPS(string imei, int mcc, int mnc, int lac, int ci, int rxlev)
{
try
{
string key = "faeed74bf5b723240d2bc8b83add4311";
Dictionary parames = new Dictionary();
parames.Add("accesstype", "0");
parames.Add("imei", imei);
parames.Add("cdma", "0");
parames.Add("network", "GPRS");
string btsstr = mcc + "," + mnc + "," + lac + "," + ci + "," + rxlev;
parames.Add("bts", btsstr);
parames.Add("nearbts", btsstr + "|" + btsstr + "|" + btsstr);
parames.Add("key", key);
Tuple parameters = GetQueryString(parames);
var carstate = Get("http://apilocate.amap.com/position", parameters.Item2);
return carstate;
}
catch (Exception ex)
{
GPSRoot request = new GPSRoot();
request.status = "error";
// LoggerHelper.Error("getLocationGPS出现错误", ex);
return request;
}
}
///
/// 用户需要登录Get请求
///
///
///
///
///
///
public static T Get(string webApi, string queryStr)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webApi + "?" + queryStr);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream streamReceive = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamReceive, Encoding.UTF8);
string strResult = streamReader.ReadToEnd();
streamReader.Close();
streamReceive.Close();
request.Abort();
response.Close();
return JsonConvert.DeserializeObject(strResult);
}
catch (Exception ex)
{
return default(T);
}
}
///
/// 拼接get参数
///
///
///
public static Tuple GetQueryString(Dictionary parames)
{
// 第一步:把字典按Key的字母顺序排序
IDictionary sortedParams = new SortedDictionary(parames);
IEnumerator> dem = sortedParams.GetEnumerator();
// 第二步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder(""); //签名字符串
StringBuilder queryStr = new StringBuilder(""); //url参数
if (parames == null || parames.Count == 0)
return new Tuple("", "");
while (dem.MoveNext())
{
string key = dem.Current.Key;
string value = dem.Current.Value;
if (!string.IsNullOrEmpty(key))
{
query.Append(key).Append(value);
queryStr.Append("&").Append(key).Append("=").Append(value);
}
}
return new Tuple(query.ToString(), queryStr.ToString().Substring(1, queryStr.Length - 1));
}
}
public class Result
{
///
///
///
public string type { get; set; }
///
///
///
public string location { get; set; }
///
///
///
public string radius { get; set; }
///
/// 江苏省 常州市 新北区 汉江中路 靠近中国建设银行(新北支行)
///
public string desc { get; set; }
///
/// 中国
///
public string country { get; set; }
///
/// 江苏省
///
public string province { get; set; }
///
/// 常州市
///
public string city { get; set; }
///
///
///
public string citycode { get; set; }
///
///
///
public string adcode { get; set; }
///
/// 汉江中路
///
public string road { get; set; }
///
/// 汉江中路
///
public string street { get; set; }
///
/// 中国建设银行(新北支行)
///
public string poi { get; set; }
}
public class GPSRoot
{
///
///
///
public string status { get; set; }
///
///
///
public string info { get; set; }
///
///
///
public string infocode { get; set; }
///
///
///
public Result result { get; set; }
}
}