示例#1
0
        /// <summary>
        /// 根据IP定位获得城市代码
        /// </summary>
        /// <returns></returns>
        public static string getCodeByIP()
        {
            //根据IP地址获取用户所在城市
            string          wUrl      = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip";
            HttpWebRequest  mRequest  = (HttpWebRequest)HttpWebRequest.Create(wUrl);
            HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();

            mRequest.ContentType = "text/html";
            mRequest.Method      = "Get";
            Stream       Streams = mResponse.GetResponseStream();
            StreamReader mReader = new StreamReader(Streams, Encoding.UTF8);
            String       mResult = mReader.ReadToEnd();

            mReader.Dispose();
            Streams.Dispose();
            mResponse.Close();
            //处理返回的json
            mResult = mResult.Substring(mResult.IndexOf("=") + 1, mResult.Length - mResult.IndexOf("=") - 2);
            //解析结果获得城市名
            DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(IP2Geo));
            MemoryStream Stream = new MemoryStream(Encoding.Unicode.GetBytes(mResult));
            IP2Geo       Ip     = (IP2Geo)Serializer.ReadObject(Stream);

            //根据城市名获得城市代码
            Xml4DB.XmlDB  mDB    = Xml4DB.XmlDBFactory.LoadXmlDB(Environment.CurrentDirectory.ToString() + "\\Config\\City.xml");
            List <object> mCitys = mDB.ReadByObject(new CityStructure(Ip.city, null));

            return(((CityStructure)mCitys[0]).CityCode);
        }
示例#2
0
文件: Form1.cs 项目: berak/cs
        private void findButton_Click(object sender, EventArgs e)
        {
            if (textBox.SelectedText != "")
            {   // check if a lat-lon was selected:
                string[] ss = textBox.SelectedText.Split(" ".ToCharArray());
                if (ss.Length == 2)
                {
                    if ((Double.Parse(ss[0]) != 0) && (Double.Parse(ss[1]) != 0))
                    {
                        ipCombo.Text = textBox.SelectedText;
                    }
                }
            }
            string lat = "", lon = "";
            textBox.Text = "";
            textBox.Enabled = false;

            // chop copy&paste blanks:
            while (ipCombo.Text.StartsWith(" "))
               ipCombo.Text = ipCombo.Text.Remove(0, 1);

            //check for ip or hostname
            string ip = findIp(ipCombo.Text);
            if (ip != null)
            {
                // check free lookup first
                string ll = getlatlon(ip);
                if (ll != null && ll != " ")
                {
                    string[] ss = ll.Split(" ".ToCharArray());
                    if (ss.Length == 2)
                    {
                        if ((Double.Parse(ss[0]) != 0) && (Double.Parse(ss[1]) != 0))
                        {
                            lat = ss[0];
                            lon = ss[1];
                        }
                        textBox.Enabled = true;
                    }
                }
                if ( lat=="" && lon=="" )
                {
                    // restricted to 40 shots a day.
                    IP2Geo geo = new IP2Geo();
                    IPInformation res = null;
                    try
                    {   // find latlon from ip or hostname:
                        res = geo.ResolveIP(ip, "0"); // 0 is the limited testkey.
                        if (res == null) return;
                        if (res.Longitude == 0 && res.Latitude == 0)
                        {
                            textBox.Text = res.Organization; // please wait or get a key ...
                            return;
                        }
                        textBox.Enabled = true;
                        textBox.Text = res.City + " (" + res.Country + ")\r\n" +
                                       "latlon:" + res.Latitude + " " + res.Longitude + "\r\n" +
                                       "ip:" + ip;
                        lat = "" + res.Latitude;
                        lon = "" + res.Longitude;
                        textBox.Text += "\r\n(fetched from http://ws.cdyne.com)\r\n";
                    }
                    catch (Exception ex) { textBox.Text += "\r\n\r\n" + ex.Message; return; }
                }
                //textBox.Text += "\r\n--- whois " + ip + "\r\n";
                //textBox.Text += whois("whois.internic.com", ip); ;
            }

            // check for latlon in input:
            if (lat == "" && lon == "")
            {
                string[] ss = ipCombo.Text.Split(" ".ToCharArray());
                if (ss.Length == 2)
                {
                    if ((Double.Parse(ss[0]) != 0) && (Double.Parse(ss[1]) != 0))
                    {
                        lat = ss[0];
                        lon = ss[1];
                    }
                }
            }
            // still not found?! check osm for place names :
            if (lat == "" && lon == "")
            {
                MapControl.SearchProvider search = new MapControl.SearchProvider();
                search.SearchCompleted += new EventHandler(osmSearchCompleted);
                if (search.Search(ipCombo.Text))
                {
                    textBox.Enabled = false;
                }
            }

            if (lat != "" && lon != "")
            {
                getMap(lat, lon);
            }
            if (!this.ipCombo.Items.Contains(this.ipCombo.Text))
                this.ipCombo.Items.Add(this.ipCombo.Text);
        }