public async Task <Location> Address(string addr) { if (string.IsNullOrEmpty(addr)) { var ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); #if DEBUG Random r = new Random(); ip = $"{r.StrictNext(210)}.{r.StrictNext(255)}.{r.StrictNext(255)}.{r.StrictNext(255)}"; #endif PhysicsAddress address = await ip.GetPhysicsAddressInfo(); if (address.Status == 0) { return(address.AddressResult.Location); } } HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") }; string s = client.GetStringAsync($"/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}").Result; var physicsAddress = JsonConvert.DeserializeAnonymousType(s, new { status = 0, result = new { location = new Location() } }); return(physicsAddress.result.location); }
public async Task <PhysicsAddress> GetIpInfo(string ip) { if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); } PhysicsAddress address = await ip.GetPhysicsAddressInfo(); return(address); }
public async Task <PhysicsAddress> GetIpInfo(string ip) { if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.UserHostAddress; } PhysicsAddress address = await ip.GetPhysicsAddressInfo(); return(address); }
public void LoginRecord(UserInfoDto userInfo, string ip) { PhysicsAddress address = ip.GetPhysicsAddressInfo(); LoginRecord record = new LoginRecord() { IP = ip, LoginTime = DateTime.Now, PhysicAddress = address.AddressResult.FormattedAddress, Province = address.AddressResult.AddressComponent.Province }; UserInfo u = UserInfoBll.GetByUsername(userInfo.Username); u.LoginRecords.Add(record); UserInfoBll.UpdateEntitySaved(u); }
public async Task <ActionResult> Address(string addr) { if (string.IsNullOrEmpty(addr)) { var ip = ClientIP; #if DEBUG Random r = new Random(); ip = $"{r.Next(210)}.{r.Next(255)}.{r.Next(255)}.{r.Next(255)}"; #endif var location = Policy <CityResponse> .Handle <AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ip)); var address = new PhysicsAddress() { Status = 0, AddressResult = new AddressResult() { FormattedAddress = IPAddress.Parse(ip).GetIPLocation().Address, Location = new Location() { Lng = location.Location.Longitude ?? 0, Lat = location.Location.Latitude ?? 0 } } }; ViewBag.Address = address.AddressResult.FormattedAddress; if (Request.Method.Equals(HttpMethods.Get) || (Request.Headers[HeaderNames.Accept] + "").StartsWith(ContentType.Json)) { return(View(address.AddressResult.Location)); } return(Json(address.AddressResult.Location)); } ViewBag.Address = addr; var physicsAddress = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}", new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token).ContinueWith(t => { if (t.IsCompletedSuccessfully) { return(JsonConvert.DeserializeObject <PhysicsAddress>(t.Result)); } return(new PhysicsAddress()); }); if (Request.Method.Equals(HttpMethods.Get) || (Request.Headers[HeaderNames.Accept] + "").StartsWith(ContentType.Json)) { return(View(physicsAddress?.AddressResult?.Location)); } return(Json(physicsAddress?.AddressResult?.Location)); }
public async Task <ActionResult> GetIpInfo(string ip) { if (string.IsNullOrEmpty(ip)) { ip = Request.UserHostAddress; } ViewBag.IP = ip; PhysicsAddress address = await ip.GetPhysicsAddressInfo(); if (Request.HttpMethod.ToLower().Equals("get")) { return(View(address)); } return(Json(address)); }
public async Task <ActionResult> GetIpInfo(string ip) { if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); } ViewBag.IP = ip; PhysicsAddress address = await ip.GetPhysicsAddressInfo(); if (Request.Method.ToLower().Equals("get")) { return(View(address)); } return(Json(address)); }
public async Task <ActionResult> Address(string addr) { if (string.IsNullOrEmpty(addr)) { var ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); #if DEBUG Random r = new Random(); ip = $"{r.StrictNext(210)}.{r.StrictNext(255)}.{r.StrictNext(255)}.{r.StrictNext(255)}"; #endif PhysicsAddress address = await ip.GetPhysicsAddressInfo(); if (address?.Status == 0) { ViewBag.Address = address.AddressResult.FormattedAddress; if (Request.Method.ToLower().Equals("get")) { return(View(address.AddressResult.Location)); } return(Json(address.AddressResult.Location)); } } ViewBag.Address = addr; using (HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") }) { var s = await client.GetStringAsync($"/geocoder/v2/?output=json&address={addr}&ak={AppConfig.BaiduAK}"); var physicsAddress = JsonConvert.DeserializeAnonymousType(s, new { status = 0, result = new { location = new Location() } }); if (Request.Method.ToLower().Equals("get")) { return(View(physicsAddress.result.location)); } return(Json(physicsAddress.result.location)); } }
public void FlushInetAddress(Interview interview) { PhysicsAddress address = interview.IP.GetPhysicsAddressInfo().Result; if (address?.Status == 0) { interview.Address = $"{address.AddressResult.FormattedAddress} {address.AddressResult.AddressComponent.Direction}{address.AddressResult.AddressComponent.Distance ?? "0"}米"; interview.Country = address.AddressResult.AddressComponent.Country; interview.Province = address.AddressResult.AddressComponent.Province; IList <string> strs = new List <string>(); address.AddressResult?.Pois?.ForEach(s => strs.Add($"{s.AddressDetail} {s.Direction}{s.Distance ?? "0"}米")); if (strs.Any()) { interview.ReferenceAddress = string.Join("|", strs); } if ("true" == CommonHelper.GetSettings("EnableDenyArea")) { CommonHelper.GetSettings("DenyArea")?.Split(',', ',').ForEach(area => { if (interview.Address.Contains(area) || (interview.ReferenceAddress != null && interview.ReferenceAddress.Contains(area))) { CommonHelper.DenyAreaIP.AddOrUpdate(area, a => new HashSet <string> { interview.IP }, (s, list) => { lock (list) { list.Add(interview.IP); return(list); } }); File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", "denyareaip.txt"), CommonHelper.DenyAreaIP.ToJsonString()); } }); } } interview.ISP = interview.IP.GetISP(); Interview i = InterviewBll.AddEntitySaved(interview); CommonHelper.InterviewCount = InterviewBll.GetAll().Count(); //记录访问量 }
public void FlushUnhandledAddress() { var list = InterviewBll.LoadEntities(i => string.IsNullOrEmpty(i.Address)).AsEnumerable(); list.ForEach(i => { PhysicsAddress addr = i.IP.GetPhysicsAddressInfo().Result; if (addr?.Status == 0) { i.Address = $"{addr.AddressResult.FormattedAddress} {addr.AddressResult.AddressComponent.Direction}{addr.AddressResult.AddressComponent.Distance}米"; i.Province = addr.AddressResult.AddressComponent.Province; IList <string> strs = new List <string>(); addr.AddressResult.Pois.ForEach(s => strs.Add($"{s.AddressDetail} {s.Direction}{s.Distance}米")); i.ReferenceAddress = string.Join("|", strs); } i.ISP = i.IP.GetISP(); InterviewBll.UpdateEntitySaved(i); }); InterviewBll.DeleteEntitySaved(i => i.IP.Contains(":") || i.IP.Equals("127.0.0.1")); }
public async Task <ActionResult> Address(string addr) { if (string.IsNullOrEmpty(addr)) { var ip = Request.UserHostAddress; #if DEBUG Random r = new Random(); ip = $"{r.StrictNext(210)}.{r.StrictNext(255)}.{r.StrictNext(255)}.{r.StrictNext(255)}"; #endif PhysicsAddress address = await ip.GetPhysicsAddressInfo(); if (address.Status == 0) { ViewBag.Address = address.AddressResult.FormattedAddress; if (Request.HttpMethod.ToLower().Equals("get")) { return(View(address.AddressResult.Location)); } return(Json(address.AddressResult.Location)); } } ViewBag.Address = addr; HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") }; string s = client.GetStringAsync($"/geocoder/v2/?output=json&address={addr}&ak={ConfigurationManager.AppSettings["BaiduAK"]}").Result; var physicsAddress = JsonConvert.DeserializeAnonymousType(s, new { status = 0, result = new { location = new Location() } }); if (Request.HttpMethod.ToLower().Equals("get")) { return(View(physicsAddress.result.location)); } return(Json(physicsAddress.result.location)); }
public async Task <PhysicsAddress> Position(string lat, string lng) { if (string.IsNullOrEmpty(lat) || string.IsNullOrEmpty(lng)) { var ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString(); #if DEBUG Random r = new Random(); ip = $"{r.StrictNext(210)}.{r.StrictNext(255)}.{r.StrictNext(255)}.{r.StrictNext(255)}"; #endif PhysicsAddress address = await ip.GetPhysicsAddressInfo(); return(address); } HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") }; string s = client.GetStringAsync($"/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak={AppConfig.BaiduAK}").Result; PhysicsAddress physicsAddress = JsonConvert.DeserializeObject <PhysicsAddress>(s); return(physicsAddress); }
public void FlushInetAddress(Interview interview) { PhysicsAddress address = interview.IP.GetPhysicsAddressInfo().Result; if (address?.Status == 0) { interview.Address = $"{address.AddressResult.FormattedAddress} {address.AddressResult.AddressComponent.Direction}{address.AddressResult.AddressComponent.Distance ?? "0"}米"; interview.Country = address.AddressResult.AddressComponent.Country; interview.Province = address.AddressResult.AddressComponent.Province; IList <string> strs = new List <string>(); address.AddressResult?.Pois?.ForEach(s => strs.Add($"{s.AddressDetail} {s.Direction}{s.Distance ?? "0"}米")); if (strs.Any()) { interview.ReferenceAddress = string.Join("|", strs); } } interview.ISP = interview.IP.GetISP(); Interview i = InterviewBll.AddEntitySaved(interview); CommonHelper.InterviewCount = InterviewBll.GetAll().Count();//记录访问量 }
public async Task <ActionResult> Position(string lat, string lng) { if (string.IsNullOrEmpty(lat) || string.IsNullOrEmpty(lng)) { var ip = Request.UserHostAddress; #if DEBUG Random r = new Random(); ip = $"{r.StrictNext(210)}.{r.StrictNext(255)}.{r.StrictNext(255)}.{r.StrictNext(255)}"; #endif PhysicsAddress address = await ip.GetPhysicsAddressInfo(); return(View(address)); } HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") }; string s = client.GetStringAsync($"/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak={ConfigurationManager.AppSettings["BaiduAK"]}").Result; PhysicsAddress physicsAddress = JsonConvert.DeserializeObject <PhysicsAddress>(s); return(View(physicsAddress)); }
public async Task <ActionResult> Position(string lat, string lng) { if (string.IsNullOrEmpty(lat) || string.IsNullOrEmpty(lng)) { var ip = ClientIP; #if DEBUG var r = new Random(); ip = $"{r.Next(210)}.{r.Next(255)}.{r.Next(255)}.{r.Next(255)}"; #endif var location = Policy <CityResponse> .Handle <AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ip)); var address = new PhysicsAddress() { Status = 0, AddressResult = new AddressResult() { FormattedAddress = IPAddress.Parse(ip).GetIPLocation().Address, Location = new Location() { Lng = location.Location.Longitude ?? 0, Lat = location.Location.Latitude ?? 0 } } }; return(View(address)); } var s = await _httpClient.GetStringAsync($"http://api.map.baidu.com/geocoder/v2/?location={lat},{lng}&output=json&pois=1&ak={AppConfig.BaiduAK}", new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token).ContinueWith(t => { if (t.IsCompletedSuccessfully) { return(JsonConvert.DeserializeObject <PhysicsAddress>(t.Result)); } return(new PhysicsAddress()); }); return(View(s)); }
public async Task <ActionResult> GetIpInfo(string ip) { if (string.IsNullOrEmpty(ip)) { ip = ClientIP; } ViewBag.IP = ip; PhysicsAddress address = await ip.GetPhysicsAddressInfo(); if (address != null && address.Status == 0) { address.AddressResult.Pois.Add(new Pois() { AddressDetail = ip.GetIPLocation() + "(本地数据库)" }); } if (Request.Method.Equals(HttpMethods.Get)) { return(View(address)); } return(Json(address)); }
/// <summary> /// 根据IP地址获取详细地理信息对象 /// </summary> /// <param name="ip"></param> /// <returns></returns> public static async Task <PhysicsAddress> GetPhysicsAddressInfo(this string ip) { ip.MatchInetAddress(out var isIpAddress); if (isIpAddress) { string ak = CoreConfig.Configuration["BaiduAK"]; if (string.IsNullOrEmpty(ak)) { throw new Exception("未配置BaiduAK,请先在您的应用程序appsettings.json中下添加BaiduAK配置节(注意大小写)"); } using (HttpClient client = new HttpClient() { BaseAddress = new Uri("http://api.map.baidu.com") }) { client.DefaultRequestHeaders.Referrer = new Uri("http://lbsyun.baidu.com/jsdemo.htm"); var task = client.GetAsync($"/location/ip?ak={ak}&ip={ip}&coor=bd09ll").ContinueWith(async t => { if (t.IsFaulted || t.IsCanceled) { return(null); } var res = await t; if (res.IsSuccessStatusCode) { var ipAddress = JsonConvert.DeserializeObject <BaiduIP>(await res.Content.ReadAsStringAsync()); if (ipAddress.Status == 0) { LatiLongitude point = ipAddress.AddressInfo.LatiLongitude; string result = client.GetStringAsync($"/geocoder/v2/?location={point.Y},{point.X}&output=json&pois=1&radius=1000&latest_admin=1&coordtype=bd09ll&ak={ak}").Result; PhysicsAddress address = JsonConvert.DeserializeObject <PhysicsAddress>(result); if (address.Status == 0) { return(address); } } else { using (var client2 = new HttpClient { BaseAddress = new Uri("http://ip.taobao.com") }) { return(await await client2.GetAsync($"/service/getIpInfo.php?ip={ip}").ContinueWith(async tt => { if (tt.IsFaulted || tt.IsCanceled) { return null; } var result = await tt; if (result.IsSuccessStatusCode) { TaobaoIP taobaoIp = JsonConvert.DeserializeObject <TaobaoIP>(await result.Content.ReadAsStringAsync()); if (taobaoIp.Code == 0) { return new PhysicsAddress() { Status = 0, AddressResult = new AddressResult() { FormattedAddress = taobaoIp.IpData.Country + taobaoIp.IpData.Region + taobaoIp.IpData.City, AddressComponent = new AddressComponent() { Province = taobaoIp.IpData.Region }, Pois = new List <Pois>() } }; } } return null; })); } } } return(null); }); return(await await task); } } return(null); }