asp.net根据ip获得城市地址
Solution 1:
专注于为中小企业提供成都网站设计、
做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业
奎文免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
#region 通过IP得到IP所在地省市
///
/// 通过IP得到IP所在地省市(Porschev)
///
///Ip地址
///
protected string GetAdrByIp(string ip) {
try
{
string url = "http://www.ip138.com/ips1388.asp?ip=" + ip + "&action=2"; string regStr = "(?<=
- .*:).*(?=\\s.*
- )"; string html = GetHtml(url);
//得到网页源码
Regex reg = new Regex(regStr, RegexOptions.None);
Match ma = reg.Match(html);
return ma.Value.Substring(ma.Value.IndexOf("省") + 1).Trim();
}
catch
{
return "";
}
}
#endregion #region 获取HTML源码信息
///
/// 获取HTML源码信息(Porschev)
///
///获取地址
///HTML源码
protected string GetHtml(string url) {
string _value = "";
HttpWebRequest req;
HttpWebResponse res;
Stream ReceiveStream;
StreamReader sr;
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.KeepAlive = false;
req.Referer = url;
req.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
res = req.GetResponse() as HttpWebResponse;
ReceiveStream = res.GetResponseStream();
sr = new StreamReader(ReceiveStream, Encoding.GetEncoding("GB2312"));
_value = HttpUtility.UrlDecode(sr.ReadToEnd());
sr.Close();
ReceiveStream.Close();
res.Close();
return _value;
}
#endregion
Tips 1:
///
/// 获取用户ip地址,避免ip代理
///
/// 用户ip地址
public string GetIp() {
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; }
if (string.IsNullOrEmpty(result))
{
result = HttpContext.Current.Request.UserHostAddress;
}
return result;
}
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
文章标题:asp.net根据ip获得城市地址-创新互联
网站路径:http://cdxtjz.cn/article/diheij.html