asp.net 通过获取IP地址 查询地理位置

lookauto 2009-03-25 10:14:35
帮忙详细解释这段代码的意思

private string m_Location = "", m_IpAddress = "", m_Response = "";

/// <summary>
/// 新建IpLocation实例以获得IP地理位置
/// </summary>
/// <param name="ipAddress"></param>
public 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(); //和这里
}

/// <summary>
/// 返回Ip地址的地理位置名称
/// </summary>
public string Location
{
get
{
return m_Location;
}
}

public string IpAddress
{
get
{
return m_IpAddress;
}
}

/// <summary>
/// 返回网络反馈原始数据
/// </summary>
public string Response
{
get
{
return m_Response;
}
}
...全文
1659 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lindardeng 2011-01-19
  • 打赏
  • 举报
回复
抓取了一个根据IP取得地址的页面内容,然后用正则匹配出地址
ssssslym 2010-08-26
  • 打赏
  • 举报
回复
xue xi
  • 打赏
  • 举报
回复
学习中...
cppfaq 2009-03-25
  • 打赏
  • 举报
回复
拿分走人:)
// 指定表单向服务器提交的编码类型,默认就上这个
client.Headers.Set("Content-Type", "application/x-www-form-urlencoded"); //尤其是这一句

// 向"http://www.ip138.com/ips.asp";页面post数据
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(); //和这里

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧