public void BinarySearchTestForChinese() { const int cityId = 2163; const string region = "中国|0|广东省|深圳市|鹏博士"; var result = _dbSearcher.BinarySearch("101.105.35.57"); Assert.AreEqual(result.CityID, cityId); Assert.AreEqual(result.Region, region); }
public void Search_Test() { string memResult = _search.MemorySearch("223.104.246.20").Region; string binarySearchResult = _search.BinarySearch("223.104.246.20").Region; string binaryTreeSearchResult = _search.BtreeSearch("223.104.246.20").Region; Assert.NotNull(memResult); Assert.NotNull(binarySearchResult); Assert.NotNull(binaryTreeSearchResult); Assert.Equal(memResult, binarySearchResult); Assert.Equal(binaryTreeSearchResult, memResult); }
public IHttpActionResult GetAuthBase(string code, string topic = "", string invite = "") { string ip = string.Empty; string region = string.Empty; string openid = string.Empty; string access_token = string.Empty; try { ip = Network.GetClientIP(); region = ipSearcher.BinarySearch(ip)?.Region; } catch { region = string.Empty; } userService = new WxUserService(); try { string[] keypair = Cache.GetCache <string[]>(code); if (keypair == null) { var http = new HttpHelper(); string oauthId = ConfigurationManager.AppSettings["oauthId"]; string oauthSecret = ConfigurationManager.AppSettings["oauthSecret"]; string apiUrl = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={oauthId}&secret={oauthSecret}&code={code}&grant_type=authorization_code"; var result = http.Post(apiUrl, string.Empty, Encoding.UTF8, Encoding.UTF8).ToJson <dynamic>(); openid = result.openid; access_token = result.access_token; if (region.Length > 100) { region = string.Empty; } userService.SaveOpenIdAndInvitation(openid, topic, invite, ip, region); Cache.WriteCache <string[]>(code, new string[] { openid, access_token }, DateTime.Now.AddHours(8)); } else { openid = keypair[0]; access_token = keypair[1]; } return(Ok(new { openid, access_token })); } catch (Exception ex) { log.Error(ex); return(InternalServerError(ex)); } }
static void Main(string[] args) { DbSearcher dbSearcher = new DbSearcher(new DbConfig { }, AppContext.BaseDirectory + @"/DBFile/ip2region.db"); string ipAddress = ""; DataBlock result = null, result2 = null, result3 = null; Console.WriteLine("请输入IP地址:"); ipAddress = Console.ReadLine(); Stopwatch sw = new Stopwatch(); do { sw.Start(); try { result = dbSearcher.BtreeSearch(ipAddress); sw.Stop(); Console.WriteLine("[btree]你的IP所属区域为:" + result.GetRegion() + ";耗时:" + sw.Elapsed.TotalMilliseconds + "ms"); sw.Start(); result2 = dbSearcher.BinarySearch(ipAddress); sw.Stop(); Console.WriteLine("[binarySearch]你的IP所属区域为:" + result2.GetRegion() + ";耗时:" + sw.Elapsed.TotalMilliseconds + "ms"); sw.Start(); result3 = dbSearcher.MemorySearch(ipAddress); sw.Stop(); Console.WriteLine("[MemorySearch]你的IP所属区域为:" + result3.GetRegion() + ";耗时:" + sw.Elapsed.TotalMilliseconds + "ms"); Console.WriteLine("请输入IP地址:"); } catch (Exception ex) { Console.WriteLine(ex.Message); sw.Stop(); } //Console.WriteLine("结束请输入bye"); } while ((ipAddress = Console.ReadLine()) != "bye"); }