【分享】
实现思路,先获取访问者ip,再通过ip调用淘宝API库,获取该ip所在的位置,即所在城市。
实体类:
[C#] 纯文本查看 复制代码 /// <summary>
/// 调用淘宝API,返回结果实体类
/// </summary>
public class TheModel
{
public int code { get; set; }
public CityModel data { get; set; }
}
/// <summary>
/// 城市实体类
/// </summary>
public class CityModel
{
public string country { get; set; }
public string country_id { get; set; }
public string area { get; set; }
public string area_id { get; set; }
public string region { get; set; }
public string region_id { get; set; }
public string city { get; set; }
public string city_id { get; set; }
public string county { get; set; }
public string county_id { get; set; }
public string isp { get; set; }
public string isp_id { get; set; }
public string ip { get; set; }
}
控制器代码:
[C#] 纯文本查看 复制代码 /// <summary>
/// 前台调用的接口
/// *在局域网下是没法玩的,需自己设置theip的值。如: 219.142.153.240*
/// </summary>
/// <returns></returns>
public JsonResult GetIPCity()
{
JsonResult json = new JsonResult();
string theip = GetWebClientIp();
string cityjson = GetstringIpAddress(theip);
CityModel city = JsonConvert.DeserializeObject<TheModel>(cityjson).data;
string thecity = city.country + city.city;
json.Data = new { theip = theip, thecity = thecity };
return json;
}
/// <summary>
/// 获取客户端ip
/// </summary>
/// <returns></returns>
private static string GetWebClientIp()
{
string userIP = "";
try
{
if (System.Web.HttpContext.Current == null
|| System.Web.HttpContext.Current.Request == null
|| System.Web.HttpContext.Current.Request.ServerVariables == null)
return "";
string CustomerIP = "";
//CDN加速后取到的IP simone 090805
CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if (!string.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (CustomerIP == null)
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.Compare(CustomerIP, "unknown", true) == 0)
return System.Web.HttpContext.Current.Request.UserHostAddress;
return CustomerIP;
}
catch { }
return userIP;
}
/// <summary>
/// 调用淘宝API获取城市信息
/// </summary>
/// <param name="strIP"></param>
/// <returns></returns>
public static string GetstringIpAddress(string theip) //strIP为IP
{
string theurl = "http://ip.taobao.com/service/getIpInfo.php?ip=" + theip + "";
string result = string.Empty;
System.Uri objurl = new System.Uri(theurl);
System.Net.WebRequest objreq;
System.Net.WebResponse objres;
System.IO.StreamReader sr;
try
{
objreq = System.Net.WebRequest.Create(objurl);
objres = objreq.GetResponse();
sr = new System.IO.StreamReader(objres.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();
objres.Close();
}
catch
{
}
finally
{
}
return result;
}
|