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; }
凡事不平凡,做好一件事就好