ip获取所在城市名称接口-JAVA
互联网有很多接口可以实现通过ip查询到具体的位置,如下:
通过淘宝IP地址库获取IP位置
用java调用淘宝ip查询接口查询地域的一个java实例:
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * 根据IP地址获取详细的地域信息
- * @project:personGocheck
- * @class:AddressUtils.java
- * @author:heguanhua E-mail:37809893@qq.com
- * @date:Nov 14, 2012 6:38:25 PM
- */
- public class AddressUtils {
- /**
- *
- * @param content
- * 请求的参数 格式为:name=xxx&pwd=xxx
- * @param encoding
- * 服务器端请求编码。如GBK,UTF-8等
- * @return
- * @throws UnsupportedEncodingException
- */
- public String getAddresses(String content, String encodingString)
- throws UnsupportedEncodingException {
- // 这里调用pconline的接口
- String urlStr = "http://ip.taobao.com/service/getIpInfo.php";
- // 从http://whois.pconline.com.cn取得IP所在的省市区信息
- String returnStr = this.getResult(urlStr, content, encodingString);
- if (returnStr != null) {
- // 处理返回的省市区信息
- System.out.println(returnStr);
- String[] temp = returnStr.split(",");
- if(temp.length<3){
- return "0";//无效IP,局域网测试
- }
- String region = (temp[5].split(":"))[1].replaceAll("\"", "");
- region = decodeUnicode(region);// 省份
- String country = "";
- String area = "";
- // String region = "";
- String city = "";
- String county = "";
- String isp = "";
- for (int i = 0; i < temp.length; i++) {
- switch (i) {
- case 1:
- country = (temp[i].split(":"))[2].replaceAll("\"", "");
- country = decodeUnicode(country);// 国家
- break;
- case 3:
- area = (temp[i].split(":"))[1].replaceAll("\"", "");
- area = decodeUnicode(area);// 地区
- break;
- case 5:
- region = (temp[i].split(":"))[1].replaceAll("\"", "");
- region = decodeUnicode(region);// 省份
- break;
- case 7:
- city = (temp[i].split(":"))[1].replaceAll("\"", "");
- city = decodeUnicode(city);// 市区
- break;
- case 9:
- county = (temp[i].split(":"))[1].replaceAll("\"", "");
- county = decodeUnicode(county);// 地区
- break;
- case 11:
- isp = (temp[i].split(":"))[1].replaceAll("\"", "");
- isp = decodeUnicode(isp); // ISP公司
- break;
- }
- }
- System.out.println(country+"="+area+"="+region+"="+city+"="+county+"="+isp);
- return region;
- }
- return null;
- }
- /**
- * @param urlStr
- * 请求的地址
- * @param content
- * 请求的参数 格式为:name=xxx&pwd=xxx
- * @param encoding
- * 服务器端请求编码。如GBK,UTF-8等
- * @return
- */
- private String getResult(String urlStr, String content, String encoding) {
- URL url = null;
- HttpURLConnection connection = null;
- try {
- url = new URL(urlStr);
- connection = (HttpURLConnection) url.openConnection();// 新建连接实例
- connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
- connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
- connection.setDoOutput(true);// 是否打开输出流 true|false
- connection.setDoInput(true);// 是否打开输入流true|false
- connection.setRequestMethod("POST");// 提交方法POST|GET
- connection.setUseCaches(false);// 是否缓存true|false
- connection.connect();// 打开连接端口
- DataOutputStream out = new DataOutputStream(connection
- .getOutputStream());// 打开输出流往对端服务器写数据
- out.writeBytes(content);// 写数据,也就是提交你的表单 name=xxx&pwd=xxx
- out.flush();// 刷新
- out.close();// 关闭输出流
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- connection.getInputStream(), encoding));// 往对端写完数据对端服务器返回数据
- // ,以BufferedReader流来读取
- StringBuffer buffer = new StringBuffer();
- String line = "";
- while ((line = reader.readLine()) != null) {
- buffer.append(line);
- }
- reader.close();
- return buffer.toString();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (connection != null) {
- connection.disconnect();// 关闭连接
- }
- }
- return null;
- }
- /**
- * unicode 转换成 中文
- *
- * @author fanhui 2007-3-15
- * @param theString
- * @return
- */
- public static String decodeUnicode(String theString) {
- char aChar;
- int len = theString.length();
- StringBuffer outBuffer = new StringBuffer(len);
- for (int x = 0; x < len;) {
- aChar = theString.charAt(x++);
- if (aChar == '\\') {
- aChar = theString.charAt(x++);
- if (aChar == 'u') {
- int value = 0;
- for (int i = 0; i < 4; i++) {
- aChar = theString.charAt(x++);
- switch (aChar) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- value = (value << 4) + aChar - '0';
- break;
- case 'a':
- case 'b':
- case 'c':
- case 'd':
- case 'e':
- case 'f':
- value = (value << 4) + 10 + aChar - 'a';
- break;
- case 'A':
- case 'B':
- case 'C':
- case 'D':
- case 'E':
- case 'F':
- value = (value << 4) + 10 + aChar - 'A';
- break;
- default:
- throw new IllegalArgumentException(
- "Malformed encoding.");
- }
- }
- outBuffer.append((char) value);
- } else {
- if (aChar == 't') {
- aChar = '\t';
- } else if (aChar == 'r') {
- aChar = '\r';
- } else if (aChar == 'n') {
- aChar = '\n';
- } else if (aChar == 'f') {
- aChar = '\f';
- }
- outBuffer.append(aChar);
- }
- } else {
- outBuffer.append(aChar);
- }
- }
- return outBuffer.toString();
- }
- // 测试
- public static void main(String[] args) {
- AddressUtils addressUtils = new AddressUtils();
- // 测试ip 219.136.134.157 中国=华南=广东省=广州市=越秀区=电信
- String ip = "125.70.11.136";
- String address = "";
- try {
- address = addressUtils.getAddresses("ip="+ip, "utf-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println(address);
- // 输出结果为:广东省,广州市,越秀区
- }
- }
ip获取所在城市名称接口-JAVA相关推荐
- C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsCC9UTk ...
- 根据IP获取具体城市名称及经纬度坐标
用到两款工具, geoip-api 这个可以根据IP获取城市以及经纬度 但是获取的城市是拼音不是汉字 导致山西和陕西的拼音相同 无法分辨,所以引入第二款工具ip2region 可以根据ip获取到具体的 ...
- 根据ip获取所在城市 php,PHP:根据IP地址获取所在城市
欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 if ( $useripFlag == chr( 2 ) ) { $AddrSeek = fread( $fd, 3 ): ...
- js通过IP地址获取所在城市
话不多说,直接贴代码! 将这段代码运行一下就OK了: <html xmlns="http://www.w3.org/1999/xhtml"> <head runa ...
- 获取地区html代码,根据ip地址获取所在城市信息(省市县)代码
[实例简介] [实例截图] [核心代码] 获取ip地址所在城市 var showIP = function(ip, remote_ip_info ){ if(remote_ip_info['ret'] ...
- H5获取用户所在城市 网页获取用户城市名称
获取用户城市名称,这里我是使用的百度地图JSAPI 2.0 文档链接 实现步骤: 1.在index.html中引用百度地图的js文件,如下:(需要使用自己的ak,获取方式:点击去官网申请ak) < ...
- php ip 城市,php如何根据IP地址获取所在城市
本文主要和大家分享php如何根据IP地址获取所在城市,本文主要以代码的形式,希望能帮助到大家.<?php header('Content-Type:text/html;Charset=utf-8 ...
- Java 通过 ip 获取所属城市信息
文章目录 1 摘要 2 核心 Maven 依赖 3 离线数据 db 保存 4 核心代码 5 测试 6 推荐参考资料 1 摘要 在实际项目中可能会用到统计用户地理位置的需求,这时可以通过客户端的 ip ...
- js获取所在城市及IP
最简单的方式获取所在城市 1.引入js: 直接在浏览器访问以下试试: http://pv.sohu.com/cityjson?ie=utf-8 2.取值: var ctiyName=returnCit ...
- uniapp|微信小程序获取当前城市名称--逆地址解析
六年代码两茫茫,不思量,自难忘 6年资深前端主管一枚,只分享技术干货,项目实战经验 关注博主不迷路~ 文章目录 问题 解决步骤 看文档 逆地址解析 1. 创建应用 2. uniapp配置 3. 代码部 ...
最新文章
- windows下的虚拟机中的ubuntu系统如何连接无线网(wifi)
- php 三个点 三角形面积,知道三角形三个顶点坐标,求面积,我觉得我的没错,但未通过,麻烦大佬帮忙看下...
- 某合资源网4.5主题 完美无错版 emlog模板
- react-native ListView 封装 实现 下拉刷新/上拉加载更多
- 51 Nod 阶乘后面0的数量
- Git - 操作指南
- 无传感FOC控制中的转子位置和速度确定方法一
- C++ 变量、常量、符号常量
- 15. Window clearTimeout() 方法
- 【spring】spring动态代理和Spring_AOP
- 2021 年“认证杯”数学中国数学建模网络挑战赛 B题
- zzulioj 1819: 加加加!(油) (打表)
- 选课系统 mysql_「选课系统」我的数据库大作业——学生选课系统实现(准备) - seo实验室...
- HC05蓝牙模块特点及使用介绍
- 【第二章】NB-IoT模组BC95入网测试
- “嗲”上海文化,女人需要学会的4种气质
- 元宇宙3D设计系统【构思与展望】
- [论文学习] - 2014ECCV - TCDCN
- 一次真实的应急响应案例(Centos)——暴力破解、替换ps命令、留多个后门——事件复现(含靶场环境)
- 服务器操作系统密码忘记,服务器操作系统密码忘记了