コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: SearchTest.cs プロジェクト: lionsoul2014/ip2region
        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);
        }
コード例 #3
0
        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));
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: zhongyoub/ip2region
        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");
        }