java通过免费接口获取ip地址的服务商信息
今天分享一个免费在线的小工具的开发代码就是通过淘宝提供的接口获取服务商信息,
工具地址:http://www.yzcopen.com/seo/ipadress
代码如下:
public class YzcPattern {
/**
* ip地址接口
*/
private final static String ipurl ="http://ip.taobao.com/service/getIpInfo.php?ip=";
/**
* 判断ip
* @param text
* @return
*/
public static boolean ipCheck(String text) {
if (text != null && !text.isEmpty()) {
// 定义正则表达式
String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
// 判断ip地址是否与正则表达式匹配
if (text.matches(regex)) {
return true;
// 返回判断信息
//return text + "\n是一个合法的IP地址!";
} else {
return false;
// 返回判断信息
//return text + "\n不是一个合法的IP地址!";
}
}
return false;
}
/**
* 读取IP的
* @param getAccessTokenUrl
* @return
*/
public static String getAuth(String getAccessTokenUrl) {
HttpURLConnection connection = null;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestProperty("User-Agent", Const.UserAgent);
connection.setRequestMethod("GET");
connection.setRequestProperty("Charsert", "UTF-8"); //设置请求编码
connection.setRequestProperty("Content-Type",
"application/json");
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 返回结果示例
*/
return result;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(connection!=null){
connection.disconnect();
}
}
return null;
}
public static void main(String[] args) throws Exception {
String ip="您的ip";
boolean bo = YzcPattern.ipCheck(ip);
if(bo){
String url = ipurl+ip;
String result = getAuth(url);
}
//获得的结果 {"code":0,"data":{"ip":"58.87.124.194","country":"中国","area":"","region":"天津","city":"天津","county":"XX","isp":"电信","country_id":"CN","area_id":"","region_id":"120000","city_id":"120100","county_id":"xx","isp_id":"100017"}}
}
}
java通过免费接口获取ip地址的服务商信息的更多相关文章
- JAVA从本机获取IP地址
JAVA从本机获取IP地址 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多博客,网上一个比较普遍的说法是通过InetAddress.getLocalHost().g ...
- 获取ip地址及城市信息
大家好,今天给大家分享的是一个简单的知识获取登录用户的ip地址及城市信息,lz是一个小白,如果有哪些错误的地方 欢迎大家指出 东西很简单,直接上代码 [HttpPost] public string ...
- thinkphp获取ip地址及位置信息
ThinkPHP\Common\funcitons.php下有一个get_client_ip()能够获取ip地址 但是有时候不够准确 找到了下面一段来 function get_client_ip($ ...
- [转] JAVA从本机获取IP地址
[From] https://www.cnblogs.com/xiaoBlog2016/p/7076230.html 论述: 此篇博客是在工作的时候,需要获得当前网络下面正确的ip地址,在网上查阅很多 ...
- java通过请求对象获取ip地址、获取ip地址
/** * 获取登录ip */ public String getIp(){ HttpServletRequest request = this.getRequest(); String ip = & ...
- 前端借助接口获取ip地址
<script language="javascript" src="http://www.codefans.net/ajaxjs/jquery1.3.2.js&q ...
- 正确JAVA从本机获取IP地址的方法
https://www.cnblogs.com/xiaoBlog2016/p/7076230.html
- PHP 获取IP地址位置信息「聚合数据API」
聚合数据 提供了[查询IP所属区域]的服务接口,只需要以 GET 请求的方式向 API 传入 IP地址 和 APPKEY 即可获得查询结果. 这里的难点主要在于如何通过PHP获取客户端IP地址,以及如 ...
- js获取IP地址方法总结_转
js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html 1,js取得IP地址 ...
随机推荐
- POJ 1159:Palindrome 最长公共子序列
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 56273 Accepted: 19455 Desc ...
- POJ 1164:The Castle
The Castle Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6677 Accepted: 3767 Descri ...
- 连接mysql的各种方式
mysql连接操作是客户端进程与mysql数据库实例进程进行通信.从程序设计角度来说,属于进程通信,常用进程通信包括: 管道.Tcp/Ip 套接字.UNIX域套接字. 1.TCP/IP (1)使用最多 ...
- Python 时间 time
import time print time.strftime("%Y-%m-%d") import datetime print datetime.datetime.now() ...
- SpringCloud学习之手把手教你用IDEA搭建入门项目(一)
SpringCloud简单搭建 jdk:1.8开发工具:IDEA注:需要了解springcloud 1.创建最简单的Maven项目 1)开始创建一个新的项目 2)创建一个空模板的maven项目,用 ...
- SpingBoot项目搭建(详细)
SpingBoot (原创:黑小子-余) springboot官网:->点击<- spring官网:->点击<- 一.SpringBoot简介 Spring Boot是由Piv ...
- python 爬虫下载英语听力新闻(npr news)为mp3格式
想通过听实时新闻来提高英语听力,学了那么多年的英语,不能落下啊,不然白费背了那么多年的单词. npr news是美国国家公共电台,发音纯正,音频每日更新,以美国为主,世界新闻为辅,比如最近我国武汉发生 ...
- RedHat无法使用yum源问题
RedHat下的yum是需要注册才能使用的 使用的话会提示: [root@test ~]# yum clean all Loaded plugins: product-id, refresh-pack ...
- Android java项目中引用kotlin,混合开发工程配置
https://www.jianshu.com/p/9220227cdfb3 buildscript { ext.kotlin_version = '1.2.71' repositories { go ...
- LUA函数闭包
词法定界:当一个函数内嵌套另一个函数的时候,内函数可以访问外部函数的局部变量,这种特征叫做词法定界 table.sort(names,functin (n1,n2) return grades[n1] ...