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的网页,然后用正则表 ...
随机推荐
- storm一些可调节的参数
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreeme ...
- http和tcp/ip,socket的区别
http协议和tcp/ip协议乍看起来,感觉是同一类的东西,其实不然,下面简单的说说他们的区别. http协议是应用层的一种数据封装协议,类似的还有ftp,telnet等等,而tcp/ip是数据传输层 ...
- 转:linux进程间通信的几种机制的比较及适用场合
源地址:http://blog.csdn.net/f_x_p0324/article/details/6878081 socket 1. # 管道( pipe ):管道是一种半双工的通信方式,数据只能 ...
- java后台使用HttpURLConnection实现百度主动推送
优点是快 不需要页面执行,,发布文章之后立即推送,所以,不管有没有人访问,都可以自动实时推送 尝试了一下httpclient,没找到相关资料,post方式无法塞url进去 最后改为 import ja ...
- [转载] OpenCV2.4.3 CheatSheet学习(二)
二.矩阵操作(拷贝.洗牌.局部访问): src.copyTo(dst) 把src矩阵中的数据拷贝到dst. src.convertTo(dst, type,scale, shift) 缩放并转换到另外 ...
- Commons BeanUtils工具包
简介: BeanUtils工具包是由Apache公司所开发,提供对Java反射和自省API的包装.其主要目的是利用反射机制对JavaBean的属性进行处理. 我们知道,一个JavaBean通常包含了大 ...
- Leetcode82. Remove Duplicates from Sorted List II删除排序链表中的重复元素2
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4->5 输出: 1-&g ...
- phpSpider 单页测试_模拟登陆
<?php require './vendor/autoload.php'; use phpspider\core\phpspider; use phpspider\core\requests; ...
- python打包成为exe文件
pyinstaller 库的使用 PyInstaller是一个十分有用的第三方库,它能够在Windows.Linux.Mac OS X 等操作系统下将 Python 源文件打包,通过对源文件打包,Py ...
- 一句话介绍python线程、进程和协程
一.进程: Python的os模块封装了常见的系统调用,其中就包括fork.而fork是linux常用的产生子进程的方法,简言之是一个调用,两个返回. 在python中,以下的两个模块用于进程的使用. ...