ASP.NET获取网络IP地址

public ActionResult Index()
        {
            // Request.UserHostAddress();
            string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
            //string IPp = "192.168.0.2";
            string city = GetMaxMindOmniData(ip);
            return View();
        }


        private string GetMaxMindOmniData(string IP)
        {
            System.Uri objUrl = new System.Uri("http://geoip.maxmind.com/e?l=YOUR_LICENSE_KEY&i=" + IP);
            System.Net.WebRequest objWebReq;
            System.Net.WebResponse objResp;
            System.IO.StreamReader sReader;
            string strReturn = string.Empty;

            try
            {
                objWebReq = System.Net.WebRequest.Create(objUrl);
                objResp = objWebReq.GetResponse();

                sReader = new System.IO.StreamReader(objResp.GetResponseStream());
                strReturn = sReader.ReadToEnd();

                sReader.Close();
                objResp.Close();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                objWebReq = null;
            }

            return strReturn;
        }

 

posted @ 2015-07-07 17:01  wancient  阅读(259)  评论(0编辑  收藏  举报