日期:2014-05-18  浏览次数:20422 次

net怎么实现根据ip获取所在您当前所在的位置
求方法:
例如:
 ip ip所在城市
 192.65.5.1 上海
 192.0.153.5 北京
 192.1.2.5 扬州
 186.5.4.6 深圳

我只有外网IP没有地址我想用IP来显示所在城市求方法.谢谢

------解决方案--------------------
google 纯真IP
------解决方案--------------------
C# code
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库都是比较常用的