获取外网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;
}
获取外网IP地址的更多相关文章
- c#获取外网IP地址的方法
1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...
- java获取外网ip地址
转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...
- C# 获取外网IP地址
很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...
- MFC C++ 获取外网IP地址
#include <afxinet.h> //GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) { U ...
- linux 获取外网ip地址
curl ifconfig.me 私有ip地址,获取公网ip
- android 根据网络来获取外网ip地址及国家,地区的接口
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- C# Winform程序获取外网IP地址
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); ...
- C#获取外网IP地址;C#获取所在IP城市地址
public static string GetIP() { using (var webClient = new WebClient()) ...
- C#获取外网IP、本机MAC地址及Ping的实现
原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...
随机推荐
- linux c数据库备份第一版
使用linuxC实现的mysql数据库备份目标:通过alarm信号定时备份数据库备注:目前是第一个版,本身不能定时备份可以结合linux自动化实现定时备份.运行平台:Linux或类unix测试平台:u ...
- Linux查看端口号
查看端口占用情况的命令:lsof -i 查看某一端口的占用情况: lsof -i:端口号 结束占用端口的进程:killall 进程名 也可使用命令: netstat -apn|grep <端口号 ...
- 在 LINQ to Entities 查询中无法构造实体或复杂类型
public List<CustomerType> GetCustomerTypesBySchemaTypeCode(int schemaTypeCode) { var query = ( ...
- Flex DataGrid 添加控件
哈喽,又和大家见面了.今天要写的东西是关于Flex DataGrid添加“编辑”或“删除”按钮. 下面是部分代码: <mx:DataGrid id="dgShow" x=&q ...
- 如何使用SplitContainer控件[转]
原文地址:http://yinzhihua2008.blog.163.com/blog/static/794306720120511150457/ 在Windows资源管理器中,当把鼠标指针移动到Tr ...
- 理解Java机制最受欢迎的8幅图
原文链接: Top 8 Diagrams for Understanding Java 翻译人员: 铁锚 翻译时间: 2013年10月29日 世间总是一图胜过千万言! 下面的8幅图来自于 Progr ...
- Android FileUtil(android文件工具类)
android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...
- DateDiff 函数,用生日获得年龄
一:截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
- mysql集群安装(centos)
mysql cluster : 1. 基于NDB Cluster 的分布式数据库系统 2. mysql集群中各服务器节点不共享数据 3. 在mysql cluster中节点指的是进程,区别于其他的集群 ...
- JavaScript 函数绑定 Function.prototype.bind
ECMAScript Edition5 IE9+支持原生,作用为将一个对象的方法绑定到另一个对象上执行. Function.prototype.bind = Function.prototype.bi ...