获取外网IP地址
public static string GetRealIP()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (null == result || result == String.Empty)
{
return "0.0.0.0";
}
return result == "127.0.0.1" ? GetUserIP() : result;
}
public static string GetUserIP()
{
WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.Default;
string response = client.UploadString("http://iframe.ip138.com/ipcity.asp", "");
Match mc = Regex.Match(response, @"location.href=""(.*)""");
response = client.UploadString(mc.Groups[1].Value, "");
int i = response.IndexOf("[") + 1;
string ip = response.Substring(i, response.IndexOf("]") - i);
string ips = ip.Replace("]", "").Replace(" ", "");
return ips;
}