C# 获取外网IP和运营商和城市
/// <summary>
/// 获取客户端外网IP,省份,城市,运营商
/// 2012年12月18日 15:07
/// </summary>
public class GetOutMessage
{
/// <summary>
/// 获取全部信息
/// </summary>
/// <returns>一段网页代码</returns>
private static string getOutMessage()
{
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
client.Headers.Set("User-Agent", "Microsoft Internet Explorer");
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, "");
return response;
}
/// <summary>
/// 外网IP
/// </summary>
/// <returns>外网IP地址</returns>
public static string getOutIp()
{
string response = getOutMessage();
int i = response.IndexOf("[") + 1;
string ip = response.Substring(i, 14);
string ips = ip.Replace("]", "").Replace(" ", "");
return ips;
}
/// <summary>
/// 省份
/// </summary>
/// <returns>省份</returns>
public static string getOutProvince()
{
string response = getOutMessage();
int i = response.IndexOf("自") + 2;
string province = response.Substring(i, response.IndexOf("省") - i + 1);
return province;
}
/// <summary>
/// 城市
/// </summary>
/// <returns>城市</returns>
public static string getOutCity()
{
string response = getOutMessage();
int i = response.IndexOf("省") + 1;
string city = response.Substring(i, response.IndexOf("市") - i + 1);
return city;
}
/// <summary>
/// 运营商
/// </summary>
/// <returns>运营商</returns>
public static string getOutProvider()
{
string response = getOutMessage();
int i = response.IndexOf("市") + 2;
string provider = response.Substring(i, 2);
return provider;
}
}
C# 获取外网IP和运营商和城市的更多相关文章
- c#获取外网IP地址的方法
1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...
- Curl之获取外网IP
获取外网IP:curl -s ifconfig.me
- .NET记录-获取外网IP以及判断该IP是属于网通还是电信
在工作时,需要获取服务器公网IP(外网IP),并且判断该IP地址是属于网通还是电信.花时间整理一下,自己实现的代码,以及后续遇到的问题. /// <summary> /// 获取外网IP ...
- Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)
Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer Advanced Installer :Free for 30 da ...
- C#获取外网IP、本机MAC地址及Ping的实现
原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...
- JAVA 优先获取外网Ip,再获取内网Ip
1.获取内网Ip private String getLocalhostIp(){ String hostAddress = ""; try { InetAddress addre ...
- linux获取外网ip
引言:目前获取ip的方法中,ifconfig和ip获取函数得到的都是内网ip.有时候需要获取外网ip,目前通用的做法,是向外部服务器发送请求,解析外部服务器响应,从而得到的自己的外网ip.linux下 ...
- java获取外网ip地址
转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...
- C# 获取外网IP地址
很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...
随机推荐
- jquery Jsonp的使用
<script type="text/javascript"> $(function(){ $.ajax({ url:"test", jsonpCa ...
- Python中的staticmethod和classmethod
谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...
- mysql系列之7.mysql读写分离
准备 下载如下linux安装包 jdk-6u31-linux-x64-rpm.bin amoeba-mysql-binary-2.2.0.tar.gz # crontab -e //同步时间 */ ...
- Wireshark学习笔记——怎样高速抓取HTTP数据包
0.前言 在火狐浏览器和谷歌浏览器中能够很方便的调试network(抓取HTTP数据包),可是在360系列浏览器(兼容模式或IE标准模式)中抓取HTTP数据包就不那么那么方便了.尽管也可使用H ...
- OpenCV Machine Learning 之 正态贝叶斯分类器 (Normal Bayes Classifier)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhjm07054115/article/details/27631913
- 剑指Offer:字符串排列【38】
剑指Offer:字符串排列[38] 题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bc ...
- Android适配API23之后权限的动态申请
一.权限介绍 对于6.0以下的权限及在安装的时候,根据权限声明产生一个权限列表,用户只有在同意之后才能完成app的安装,造成了我们想要使用某个app,就要默默忍受其一些不必要的权限(比如是个app都要 ...
- 数据结构ADT是什么
抽象数据类型的缩写 abstract data type .表示数据结构的抽象模型.数据结构是一个数据概念的定义,通过各种工具对数据结构的概念类型的描述称之为抽象数据类型,简单地说是指一个数学模型以及 ...
- 话说文件系统——aufs源码分析(三)【转】
本文转载自:http://www.cnblogs.com/xie0812/p/8848185.html 1. linux中设备驱动的入口都是:module_init(xxx_init);里面注册的函数 ...
- 【LeetCode】两个有序数组合成一个有序数组
a = [1,3,5,7,9]b = [2,4,6,8,10]c= [] while len(a) > 0: if len(b) == 0: c.extend(a) break min_num ...