曾几何时通过IP获取相应所在地的地址一直是一个梦想,曾经为止努力过,但效果并不是很好,现在将努力的成果(某些来源于网络)粘贴出来,希望对某些人有所启发,倘若有人有新的见解,还望不惜赐教:

1、借助外网实现获取IP相应所在地地址的目的:

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;public class IpReport {/*** 对服务器端返回的XML进行解析* * @return 字符串 用,分割*/public static String getIp(String ip) {try {DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setNamespaceAware(true);DocumentBuilder db = dbf.newDocumentBuilder();InputStream is = getSoapInputStream(ip);Document doc = db.parse(is);NodeList nodeList = doc.getElementsByTagName("string");StringBuffer stringBuffer = new StringBuffer();for (int count = 0; count < nodeList.getLength(); count++) {Node n = nodeList.item(count);if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) {stringBuffer = new StringBuffer("#") ;break ;}stringBuffer.append(n.getFirstChild().getNodeValue() + "#");//System.out.println(":"+n.getFirstChild().getNodeValue());}is.close();return stringBuffer.toString().split("#")[1];} catch (Exception e) {e.printStackTrace();return null;}}/*** 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流 用户输入的城市名称* * @return 服务器端返回的输入流,供客户端读取 */private static InputStream getSoapInputStream(String ip) throws Exception {try {String soap = getSoapRequest(ip);if (soap == null) {return null;}URL url = new URL("http://webservice.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx");URLConnection conn = url.openConnection();conn.setUseCaches(false);conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");conn.setRequestProperty("SOAPAction","http://WebXml.com.cn/getCountryCityByIp");OutputStream os = conn.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");osw.write(soap);osw.flush();osw.close();InputStream is = conn.getInputStream();return is;} catch (Exception e) {e.printStackTrace();return null;}}/*** 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市* * @return 客户将要发送给服务器的SOAP请求*/private static String getSoapRequest(String ip) {StringBuilder sb = new StringBuilder();sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+ "<soap:Body>    " + "<getCountryCityByIp xmlns=\"http://WebXml.com.cn/\">"+ "<theIpAddress>" + ip   + "</theIpAddress>" + "</getCountryCityByIp>"+ "</soap:Body></soap:Envelope>");return sb.toString();}public static void main(String[] args) throws Exception {System.out.println(getIp("218.25.139.160"));//输出结果:辽宁省大连市 联通ADSLSystem.out.println(getIp("200.151.111.111"));//输出结果:巴西 圣保罗System.out.println(getIp("125.13.13.4"));//输出结果:日本 ATHOME网络System.out.println(getIp("58.16.209.19"));//输出结果:贵州省六盘水市 六枝特区腾龙网吧System.out.println(getIp("22.8.129.60"));//输出结果:美国 DoD网络信息中心}
}

2、借助qqwry.dat文件实现获取IP相应所在地地址的目的:

由于借助qqwry.dat文件实现获取IP所属地的代码比较多,这里仅给出下载该java工程的地址:

0分下载Demo工程

通过IP获取相应所在地的地址相关推荐

  1. 百度接口通过ip获取用户所在地

    以下是我看到的资料,我在引擎中用插件连接了"http://opendata.baidu.com/api.php?query=" + strIP+"&co=& ...

  2. PHP根据IP获取当前所在地地址

    原理简述: 利用"ip.taobao.com"提供的一个功能,先获取json格式字符串,再解码json字符串 新建文件getaddress.class.php: <?php ...

  3. 获取本机外网地址和ip地址所在地查询

    获取本机外网地址 站长之家 ( 返回格式: json) : http://ip.chinaz.com/getip.aspx 搜狐 ( 返回格式: js) : http://pv.sohu.com/ci ...

  4. java 根据ip获取mac地址_利用java如何根据IP获取mac地址

    利用java如何根据IP获取mac地址 发布时间:2020-11-25 15:49:42 来源:亿速云 阅读:74 作者:Leah 利用java如何根据IP获取mac地址?针对这个问题,这篇文章详细介 ...

  5. java-通过ip获取地址

    添加maven依赖 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all< ...

  6. java通过ip获取网卡MAC地址

    参考:http://www.jb51.net/article/73882.htm /** * 获取MAC地址 * @author * 2011-12 */ public static class Ge ...

  7. 通过IP获取地址,限制某些地址访问(GeoIp)

    文章目录 一.前言 二.其他免费库 三.GeoIp库 1.下载对应的mmdb文件,使用github上的库来读取 (1)mmdb文件下载地址: (2)github库下载地址 2.使用geoip的扩展来读 ...

  8. android 获取网卡mac_Android获取本机Mac地址及IP地址方法

    1.Android  获取本机Mac 地址方法: 需要在AndroidManifest.xml文件中添加权限: public String getLocalMacAddress() { WifiMan ...

  9. linux跨ip拷贝,Linux的虚拟机拷贝到另外的操作系统时,NAT方式的静态IP无效,一直是获取的DHCP动态地址...

    一.问题: 1.Linux虚拟机在A机的虚拟机中是NAT方式DHCP动态获取IP,设置了静态地址,IP地址不会发生变化,但是拷贝到B机上的虚拟机后,一直获取的时动态IP地址,无法得到静态IP. 2.执 ...

  10. 获取客户端网卡MAC地址和IP地址的几种方法(一)

    获取客户端网卡MAC地址和IP地址的几种方法(一) 作者:代码乱了 在做B/S结构的系统时,我们常常需要获取客户端的一些信息,如IP和MAC,以结合身份验证.在ASP.NET中,要获取服务器端的MAC ...

最新文章

  1. Camera开发系列之六-使用mina框架实现视频推流
  2. 图像处理(五)双指数磨皮
  3. bnpparibas
  4. CodeIgniter模型
  5. 详解实时查看网卡流量的几款工具
  6. 1分钟爬取全国高校信息,制成大屏可视化!
  7. ASP.NET Core Razor页面 vs MVC
  8. Java生态系统– 2014年我的5大亮点
  9. 手机发送验证码的业务逻辑探究-主要是安全性,响应性
  10. java游戏将相_(Java)算法——位运算基础及基本应用
  11. Spring Cloud Hystrix——熔断器
  12. 【队列+模拟】机器翻译 luogu-1540
  13. 声音存储空间计算公式_声音采样频率计算公式
  14. 解析MOS管推挽电路组成结构和特征优缺点
  15. 华为服务器怎么查看系统日志,云服务器怎么查看系统日志
  16. iterative(迭代的) 和recursive(递归的)的区别
  17. [附源码]计算机毕业设计Node.js-报刊征订管理系统(程序+LW)
  18. local-preference-实践理解(22/3/2)
  19. 微信游戏奇迹暖暖选取服务器失败,奇迹暖暖微信区为什么登不上_奇迹暖暖微信区登不上解决办法-66街机网...
  20. <<算法很美>>——(三)十大排序算法(上)

热门文章

  1. 谈谈持久连接——HTTP权威指南读书心得(五)
  2. ‘numeric_limits’ is not a member of ‘std‘解决方法
  3. redis string底层数据结构
  4. Node学习记录: mongodb
  5. Hyper-V网络虚拟化--VM之间拷贝速度慢
  6. hdu 1242 Rescue(BFS入门)
  7. drbd+corosync+pacemaker实现mysql的高可用性“上”
  8. The New iPad’s Achilles Heel
  9. 脱离 Windows 完全使用 Linux你花了多少时间适应?
  10. 3.Linux 高性能服务器编程 --- TCP 协议详解