asp. net Instance code to get the IP address accessed by the client browser
- 2021-10-13 07:01:34
- OfStack
This article introduces the asp. net access to the client browser IP address example code, to share with you, also leave a note
1. js method
<!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 Address :' + returnCitySN["cip"] + ', City code :' + returnCitySN["cid"] + ', Region :' + returnCitySN["cname"]);
</script>
</head>
<body>
</body>
</html>
2. Background code implementation
#region IP Address restriction function 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 (" Beijing ".Equals(addr[0]) || " Beijing ".Equals(addr[1]))
{
flag = true;
}
return flag;
}
/// <summary>
/// Object of the remote access user Ip Address
/// </summary>
/// <returns> Return Ip Address </returns>
protected string GetLoginIp()
{
string loginip = "";
//Request.ServerVariables[""]-- Gets a collection of service variables
if (Request.ServerVariables["REMOTE_ADDR"] != null) // Of the remote host making the request ip Whether the address is empty
{
// Object of the remote host that made the request Ip Address
loginip = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
// Determining whether the registered user uses the setup agent
else if (Request.ServerVariables["HTTP_VIA"] != null)
{
if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
// Get the server of the proxy Ip Address
loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
// Get client IP
loginip = Request.UserHostAddress;
}
}
else
{
// Get client IP
loginip = Request.UserHostAddress;
}
return loginip;
}
/// <summary>
/// According to IP Get provinces and cities
/// </summary>
public string[] GetAddressByIp(string ip)
{
string PostUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=" + ip;
string res = GetDataByPost(PostUrl);// The data returned by this request is: res=1t115.193.210.0t115.194.201.255t China t Zhejiang t Hangzhou t Telecommunications
string[] arr = getAreaInfoList(res);
return arr;
}
/// <summary>
/// Post Request data
/// </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>
/// Processing the desired data
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static string[] getAreaInfoList(string ipData)
{
//1t115.193.210.0t115.194.201.255t China t Zhejiang t Hangzhou t Telecommunications
string[] areaArr = new string[10];
string[] newAreaArr = new string[2];
try
{
// Take the required data, only the provinces and cities are taken here
areaArr = ipData.Split('t');
newAreaArr[0] = areaArr[4];// Province
newAreaArr[1] = areaArr[5];// City
}
catch (Exception e)
{
}
return newAreaArr;
}
#endregion