protected void Page_Load(object sender, EventArgs e)
{
//获取ip
if (Context.Request.ServerVariables["HTTP_VIA"] != null) // using proxy
{
ip.Text = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// not using proxy or can't get the Client IP
{
ip.Text = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
// GetIpAddress(ip.Text);
GetIpAddress(ip.Text,0);
GetCitycode(address.Text);
}
//根据ip获取所在的城市
private void GetIpAddress(string ip)
{
string url = " http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2";//210.51.45.99
string pageAll = GetHtml(url, Encoding.Default);
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<li>本站主数据:(.*?)</li>", System.Text.RegularExpressions.RegexOptions.None);
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
string link = string.Empty;
foreach (System.Text.RegularExpressions.Match ma in mc)
{
address .Text= ma.Groups[1].Value;
}
}
//根据ip获取所在的城市
private void GetIpAddress(string ip)
{
string url = " http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + "210.51.45.99";//210.51.45.99
string pageAll = GetHtml(url, Encoding.Default);
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<location>(.*?)</location>", System.Text.RegularExpressions.RegexOptions.None);
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
string link = string.Empty;
foreach (System.Text.RegularExpressions.Match ma in mc)
{
address.Text = ma.Groups[1].Value;
}
}
private string GetHtml(string url, Encoding encoding)
{
System.Net.HttpWebResponse response = null;
System.IO.Stream dataStream = null;
System.IO.StreamReader reader = null;
try
{
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Timeout = 300000;
response = (System.Net.HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
reader = new System.IO.StreamReader(dataStream, encoding);
return reader.ReadToEnd();
}
catch (Exception ex)
{
return "";
}
}
{
//获取ip
if (Context.Request.ServerVariables["HTTP_VIA"] != null) // using proxy
{
ip.Text = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// not using proxy or can't get the Client IP
{
ip.Text = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
// GetIpAddress(ip.Text);
GetIpAddress(ip.Text,0);
GetCitycode(address.Text);
}
//根据ip获取所在的城市
private void GetIpAddress(string ip)
{
string url = " http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2";//210.51.45.99
string pageAll = GetHtml(url, Encoding.Default);
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<li>本站主数据:(.*?)</li>", System.Text.RegularExpressions.RegexOptions.None);
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
string link = string.Empty;
foreach (System.Text.RegularExpressions.Match ma in mc)
{
address .Text= ma.Groups[1].Value;
}
}
//根据ip获取所在的城市
private void GetIpAddress(string ip)
{
string url = " http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + "210.51.45.99";//210.51.45.99
string pageAll = GetHtml(url, Encoding.Default);
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<location>(.*?)</location>", System.Text.RegularExpressions.RegexOptions.None);
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
string link = string.Empty;
foreach (System.Text.RegularExpressions.Match ma in mc)
{
address.Text = ma.Groups[1].Value;
}
}
private string GetHtml(string url, Encoding encoding)
{
System.Net.HttpWebResponse response = null;
System.IO.Stream dataStream = null;
System.IO.StreamReader reader = null;
try
{
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Timeout = 300000;
response = (System.Net.HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
reader = new System.IO.StreamReader(dataStream, encoding);
return reader.ReadToEnd();
}
catch (Exception ex)
{
return "";
}
}