C#根据IP获取省市
/// <summary>
/// 根据IP获取省市
/// </summary>
public void GetAddressByIp()
{
string ip = "115.193.217.249";
string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;
string res = GetDataByPost(PostUrl);//该条请求返回的数据为:res=1\t115.193.210.0\t115.194.201.255\t中国\t浙江\t杭州\t电信
string[] arr = getAreaInfoList(res);
}
/// <summary>
/// Post请求数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string GetDataByPost(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string s = "anything";
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(s);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd();
sr.Close();
res.Close();
return backstr;
}
/// <summary>
/// 处理所要的数据
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static string[] getAreaInfoList(string ipData)
{
//1\t115.193.210.0\t115.194.201.255\t中国\t浙江\t杭州\t电信
string[] areaArr = new string;
string[] newAreaArr = new string;
try
{
//取所要的数据,这里只取省市
areaArr = ipData.Split('\t');
newAreaArr = areaArr;//省
newAreaArr = areaArr;//市
}
catch (Exception e)
{
// TODO: handle exception
}
return newAreaArr;
}
111 代码可以使用 一般用来干啥呢, 即墨还雀 发表于 2016-8-29 09:33
一般用来干啥呢,
用户登录的时候 记录在哪个城市登录的 我就来装个逼{:3_41:} 卖烤地瓜的 发表于 2016-8-29 10:10
我就来装个逼
..... ibcadmin 发表于 2016-8-29 09:36
用户登录的时候 记录在哪个城市登录的
{:2_35:}
页:
[1]