java获取外网ip地址
转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/
/**
* 获取外网IP、归属地、操作系统
* @return
*/
public static String[] getIp(){
String[] obj = new String[3];
StringBuffer strForeignIP = new StringBuffer("");
StringBuffer strLocation = new StringBuffer("");
StringBuffer strOperatorMessageation = new StringBuffer("");
String ipAddress = "";
String ipLocation = "";
String ipSystem = "";
StringBuffer strUrl =new StringBuffer("http://www.cz88.net/ip/viewip778.aspx");
try{
URL url = new URL(strUrl.toString());
URLConnection context = url.openConnection();
InputStream in = context.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "gb2312"));//防止读出来的是乱码
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
br.close();
String webContent = sb.toString();
if (null!=webContent && webContent.trim().length()>0){
String flagofForeignIPString = "IPMessage";
int startIP = webContent.indexOf(flagofForeignIPString) + flagofForeignIPString.length() + 2;
int endIP = webContent.indexOf("</span>", startIP);
strForeignIP.delete(0, webContent.length());
strForeignIP.append(webContent.substring(startIP, endIP));
String flagofLocationString = "AddrMessage";
int startLoc = webContent.indexOf(flagofLocationString)+ flagofLocationString.length() + 2;
int endLoc = webContent.indexOf("</span>", startLoc);
strLocation.delete(0, webContent.length());
strLocation.append(webContent.substring(startLoc, endLoc));
String flagoOperatorMessage = "OperatorMessage";
int startOpera = webContent.indexOf(flagoOperatorMessage)+ flagoOperatorMessage.length() + 2;
int endOpera = webContent.indexOf("</span>", startOpera);
strOperatorMessageation.delete(0, webContent.length());
strOperatorMessageation.append(webContent.substring(startOpera, endOpera));
}
}catch(Exception e){
System.out.println("IpUtil:get ip is failed:"+e.getMessage());
e.printStackTrace();
}
if(strForeignIP.toString().trim().length()<0){
ipAddress = "未知";
}else{
ipAddress = strForeignIP.toString();
}
if(strForeignIP.toString().trim().length()<0){
ipLocation = "未知";
}else{
ipLocation = strLocation.toString();
}
if(strForeignIP.toString().trim().length()<0){
ipSystem = "未知";
}else{
ipSystem = strOperatorMessageation.toString();
}
obj[0]=ipAddress;
obj[1]=ipLocation;
obj[2]=ipSystem;
return obj;
}
java获取外网ip地址的更多相关文章
- c#获取外网IP地址的方法
1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...
- 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); ...
- 获取外网IP地址
public static string GetRealIP(){ string result = String.Empty; result = HttpC ...
- 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的网页,然后用正则表 ...
随机推荐
- spark编程入门-idea环境搭建
原文引自:http://blog.csdn.net/huanbia/article/details/69084895 1.环境准备 idea采用2017.3.1版本. 创建一个文件a.txt 2.构建 ...
- 2018-12-18-WPF-一个空的-WPF-程序有多少个窗口
title author date CreateTime categories WPF 一个空的 WPF 程序有多少个窗口 lindexi 2018-12-18 21:16:40 +0800 2018 ...
- istringstream字符串流对象
1.读取字符串流对象 istringstream类用于执行C++风格的字符串流的输入操作. ostringstream类用于执行C++风格的字符串流的输出操作. strstream类同时可以支持C++ ...
- Apache SOLR and Carrot2集成
1.环境 下载软件 名称 地址 solr-integration-strategies-gh-pages.zip https://github.com/carrot2/solr-integration ...
- leyou_07_对数据的操作
1.目标在数据库的两张表中拿到以下数据,并完成状态.搜索和分页功能 实体类Spu(页面需要的数据) 实体类Category(页面需要的数据) name:商品分类 2.分析: 返回的数据在两个实体类中, ...
- utils03_clone远程仓库
1.Bash here 克隆方式 复制要克隆远程仓库的SSH或者HTTPS 使用Bash here克隆文件 2.使用git同步
- HTML5移动开发中的input输入框类型 (转)
公司的项目开发过程中的,的用户体验忽略了.登录tel就用tel属性.新来的小伙伴提醒的.谢谢他 数字类型number 定义input类型为type="number"时,iOS显示数 ...
- 工控安全入门(四)—— DNP3协议
我们之前看过了法国施耐德的Modbus.德国西门子的S7comm,这次就让我们把目光投到美洲,看看加拿大的HARRIS的DNP3有什么特别之处. 这次选用的流量包部分来自w3h的gitbub: htt ...
- 新闻内页 上一篇写一篇问题,ID不连续,不用链表
y要什么链表? 用sql查询上一篇 SELECT id,title FROM t_article WHERE id<10 ORDER BY id DESC LIMIT 1; 用sql查下一篇 S ...
- jsonp 请求报Uncaught SyntaxError: Unexpected token :
$(document).ready(function() { jQuery.ajax({ type: 'GET', url: 'http://wncrunners.com/admin/colors.j ...