Exemplo n.º 1
0
        int searchType = 0;//查询类型:0,离线;1,在线(百度);

        /// <summary>
        /// 离线查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnIPSeach_Click(object sender, EventArgs e)
        {
            searchType = 0;
            if (txtIPList.Text == "")
            {
                MessageBox.Show("请输入要查找的IP地址");
                return;
            }

            //校验IP列表的合法性
            Regex reg = new Regex(@"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)");

            //按每一行进行查找
            IP.EnableFileWatch = true;
            IP.Load("17monipdb.dat");

            string resultMsg = "";

            string[] ipArr = txtIPList.Text.Split('\n');
            for (int i = 0; i < ipArr.Length; i++)
            {
                if (ipArr[i] != "")
                {
                    try
                    {
                        string ip = reg.Match(ipArr[i]).Value;

                        resultMsg += ip + "," + string.Join(",", IP.Find(ip)) + "\r\n";
                    }
                    catch
                    {
                        resultMsg += ipArr[i] + ",不合法的IP地址,,,\r\n";
                    }
                }
                else
                {
                    resultMsg += ",,,,\r\n";
                }
            }
            txtResult.Text = resultMsg;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 查询本机IP
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLocalIP_Click(object sender, EventArgs e)
        {
            //http://api.ipip.net/api.php?a=ipdb
            //113.119.134.38|20150801|http://s.qdcdn.com/17mon/17monipdb.dat

            IP.EnableFileWatch = true;
            IP.Load("17monipdb.dat");
            string getIPDBUrl = "http://api.ipip.net/api.php?a=ipdb";

            WebRequest  webReq  = WebRequest.Create(getIPDBUrl);
            WebResponse webResp = webReq.GetResponse();
            Stream      stream  = webResp.GetResponseStream();

            StreamReader sr   = new StreamReader(stream, Encoding.UTF8);
            string       html = sr.ReadToEnd();

            sr.Close();
            stream.Close();

            string ip = html.Split('|')[0];

            txtResult.Text = ip + "," + string.Join(",", IP.Find(ip)) + "\r\n";
        }