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;
 }

获取外网IP地址的更多相关文章

  1. c#获取外网IP地址的方法

    1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...

  2. java获取外网ip地址

    转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...

  3. C# 获取外网IP地址

    很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...

  4. MFC C++ 获取外网IP地址

    #include <afxinet.h> //GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) { U ...

  5. linux 获取外网ip地址

    curl ifconfig.me 私有ip地址,获取公网ip

  6. android 根据网络来获取外网ip地址及国家,地区的接口

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...

  7. C# Winform程序获取外网IP地址

    string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); ...

  8. C#获取外网IP地址;C#获取所在IP城市地址

    public static string GetIP()         {             using (var webClient = new WebClient())           ...

  9. C#获取外网IP、本机MAC地址及Ping的实现

    原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...

随机推荐

  1. DOS命令行使用pscp实现远程文件和文件夹传输(转)

    转自 http://snailwarrior.blog.51cto.com/680306/141201   pscp是putty安装包所带的远程文件传输工具,使用和Linux下scp命令相似,具体的使 ...

  2. 151111 sqlite3数据库学习

    最近在学习数据库,想起去年做的项目里用到了sqlite3.那时候,没有任何的数据库经验,误打误撞,找到了sqlite3,然后参考网络上零碎的信息,把它嵌入到工程里,并且成功了.可惜,那时候没有好好保存 ...

  3. 转:PHP开发者应了解的24个库

    原文来自于:http://blog.jobbole.com/54201/ 作为一个PHP开发者,现在是一个令人激动的时刻.每天有许许多多有用的库分发出来,在Github上很容易发现和使用这些库.下面是 ...

  4. Agri-Net

    poj1258:http://poj.org/problem?id=1258 题意:生成树的模板题.简单题. #include<iostream> #include<cstring& ...

  5. a trick in reading and storing file in the exact way!

    read and write file is a very common operation regarding file mainuplation. However, the powerfull g ...

  6. BestCoder Round #51 (div.2)

    明显是无良心的数学round= = 1000 Zball in Tina Town #include<iostream> #include<cstdio> #include&l ...

  7. 5451 HDU Best Solver

    链接: Best Solver 题目分析: 这个题目的关键点是需知道“共轭”. 如 :(A√B + C√D)  和 (A√B - C√D) 是共轭的 这个有一个规律 (A√B + C√D)^n + ( ...

  8. 【转】Android低功耗蓝牙应用开发获取的服务UUID

    原文网址:http://blog.csdn.net/zhangjs0322/article/details/39048939 Android低功耗蓝牙应用程序开始时获取到的蓝牙血压计所有服务的UUID ...

  9. PHP删除数组中特定元素

    方法一: <?php $arr1 = array(1,3, 5,7,8); $key = array_search(3, $arr1); if ($key !== false) array_sp ...

  10. (转载)URL与URI的区别

    (转载)http://blog.csdn.net/eagle51998/article/details/372052 1  URL(Uniform Resoure Locator:统一资源定位器)是W ...