CSkin博客
标题: 【系统操作篇】取本机内,外网IP [打印本页]
作者: cskin 时间: 2014-8-1 21:38
标题: 【系统操作篇】取本机内,外网IP
[C#] 纯文本查看 复制代码
//得到本机内网IP
string s = "";//定义一个变量,用来记录IP地址
System.Net.IPAddress[] addressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;//获取IP地址列表
for (int i = 0; i < addressList.Length; i++)//遍历IP地址列表
{
s += addressList.ToString() + "\n";//获得遍历到的IP地址
}
//s就是本机内网ip
[C#] 纯文本查看 复制代码
/// <summary>
/// 获得本机公网IP
/// </summary>
/// <returns>获取不到则返回空串</returns>
private static string GetIP()
{
string tempip = "";
try
{
WebRequest wr = WebRequest.Create("http://www.ip138.com/ips138.asp");
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int start = all.IndexOf("您的IP地址是:[") + 9;
int end = all.IndexOf("]", start);
tempip = all.Substring(start, end - start);
sr.Close();
s.Close();
}
catch
{
}
return tempip;
}
作者: xiaobo 时间: 2014-8-1 21:46
推荐另外一个新浪的IP查询API:
http://int.dpool.sina.com.cn/iplookup/iplookup.php
[AppleScript] 纯文本查看 复制代码
1 60.211.128.0 60.211.255.255 中国 山东 济宁 联通
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=Json
[XML] 纯文本查看 复制代码
{"ret":1,"start":"60.211.128.0","end":"60.211.255.255","country":"\u4e2d\u56fd","province":"\u5c71\u4e1c","city":"\u6d4e\u5b81","district":"","isp":"\u8054\u901a","type":"","desc":""}
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=Js
[JavaScript] 纯文本查看 复制代码
var remote_ip_info = {"ret":1,"start":"60.211.128.0","end":"60.211.255.255","country":"\u4e2d\u56fd","province":"\u5c71\u4e1c","city":"\u6d4e\u5b81","district":"","isp":"\u8054\u901a","type":"","desc":""};
作者: MeeSii 时间: 2014-8-6 10:10
获取外网IP一定要借助WEB页面吗。。就没有不访问WEB的方法?
作者: tjf0303 时间: 2015-2-9 12:30
用的是访问ip138,然后抓取内容分析的原理啊,也不错,呵呵
作者: mumupudding 时间: 2015-2-10 10:19
不错有用,值得学习
作者: gts 时间: 2016-9-26 02:00
谢谢提供有用的代码。
作者: esf5021314 时间: 2016-10-21 16:02
谢谢楼猪分享,楼猪辛苦了~
欢迎光临 CSkin博客 (http://bbs.cskin.net/) |
Powered by Discuz! X3.2 |