日期:2014-05-18 浏览次数:20452 次
public partial class Test_IP : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String user_IP = ""; if (Request.ServerVariables["HTTP_VIA"] != null) { user_IP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); } else { user_IP = Request.ServerVariables["REMOTE_ADDR"].ToString(); } this.Label1.Text ="ip="+user_IP+",,,"+ IpLocation(user_IP); } private string m_Location = "", m_IpAddress = "", m_Response = ""; public string IpLocation(string ipAddress) { m_IpAddress = ipAddress.Trim(); string[] ip = ipAddress.Split('.'); ipAddress = ip[0] + "." + ip[1] + ".1.1"; WebClient client = new WebClient(); client.Encoding = System.Text.Encoding.GetEncoding("GB2312"); string url = "http://www.ip138.com/ips.asp"; string post = "ip=" + ipAddress + "&action=2"; client.Headers.Set("Content-Type", "application/x-www-form-urlencoded"); string response = client.UploadString(url, post); m_Response = response; string p = @"<li>参考数据一:(?<location>[^<>]+?)</li>"; Match match = Regex.Match(response, p); m_Location = match.Groups["location"].Value.Trim(); return m_Location; } public string Location { get { return m_Location; } } public string IpAddress { get { return m_IpAddress; } } /// <summary> /// 返回网络反馈原始数据 /// </summary> public string Response { get { return m_Response; } } }
------解决方案--------------------
http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?op=getCountryCityByIp
------解决方案--------------------
下个IP地址库,然后找。
1楼说的那个和珊瑚虫IP库都是比较常用的