本文介绍了asp.net 获取客户端浏览器访问的IP地址的实例代码,分享给大家,自己也留个笔记
1、js方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html> < html > < head > < meta charset = "UTF-8" /> < title >Document</ title > < script src = "http://pv.sohu.com/cityjson?ie=utf-8" ></ script > < script type = "text/javascript" > document.write('IP地址:' + returnCitySN["cip"] + ', 城市编码:' + returnCitySN["cid"] + ', 地区:' + returnCitySN["cname"]); </ script > </ head > < body > </ body > </ html > |
2、后台代码实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#region IP地址限制功能 2017-07-18 /// <summary> /// /// </summary> /// <returns></returns> public bool IsIPValidate() { bool flag = false ; string userip = GetLoginIp(); string [] addr = GetAddressByIp(userip); string addrs = addr[0] + addr[1]; if ( "北京" .Equals(addr[0]) || "北京" .Equals(addr[1])) { flag = true ; } return flag; } /// <summary> /// 获取远程访问用户的Ip地址 /// </summary> /// <returns>返回Ip地址</returns> protected string GetLoginIp() { string loginip = "" ; //Request.ServerVariables[""]--获取服务变量集合 if (Request.ServerVariables[ "REMOTE_ADDR" ] != null ) //判断发出请求的远程主机的ip地址是否为空 { //获取发出请求的远程主机的Ip地址 loginip = Request.ServerVariables[ "REMOTE_ADDR" ].ToString(); } //判断登记用户是否使用设置代理 else if (Request.ServerVariables[ "HTTP_VIA" ] != null ) { if (Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ] != null ) { //获取代理的服务器Ip地址 loginip = Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ].ToString(); } else { //获取客户端IP loginip = Request.UserHostAddress; } } else { //获取客户端IP loginip = Request.UserHostAddress; } return loginip; } /// <summary> /// 根据IP获取省市 /// </summary> public string [] GetAddressByIp( string ip) { string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip; string res = GetDataByPost(PostUrl); //该条请求返回的数据为:res=1t115.193.210.0t115.194.201.255t中国t浙江t杭州t电信 string [] arr = getAreaInfoList(res); return arr; } /// <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) { //1t115.193.210.0t115.194.201.255t中国t浙江t杭州t电信 string [] areaArr = new string [10]; string [] newAreaArr = new string [2]; try { //取所要的数据,这里只取省市 areaArr = ipData.Split( 't' ); newAreaArr[0] = areaArr[4]; //省 newAreaArr[1] = areaArr[5]; //市 } catch (Exception e) { } return newAreaArr; } #endregion |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/zdw_wym/article/details/75308133?utm_source=tuicool&utm_medium=referral