根據ip地址查詢所在地

public string IpLocation(string ipAddress) 
    {
        string[] result;
        if (string.IsNullOrEmpty(ipAddress.Trim()))
        {
            return null;
        }
        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);                             
 
        string p = @"<li>參考數據二:(?<location>[^<>]+?)</li>";
        Match match = Regex.Match(response, p);                                            
        string m_Location = match.Groups["location"].Value.Trim();
        result = m_Location.Split(' ');                    
        return result[0];
    }

方法來源:http://bbs.csdn.net/topics/360101553


方法二:http://blog.csdn.net/aspnet2002web/article/details/7561654



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章