通过IP获取对应所在地的地址
曾几何时通过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 {
/**
* 对server端返回的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请求发送给server端,并返回server点返回的输入流 用户输入的城市名称
*
* @return server端返回的输入流,供client读取
*/
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 客户将要发送给server的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"));//输出结果:辽宁省大连市 联通ADSL
System.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所属地的代码比較多。这里仅给出下载该javaproject的地址:
通过IP获取对应所在地的地址的更多相关文章
- 百度接口通过ip获取用户所在地
/** * 百度接口 * 通过用户ip获取用户所在地 * @param userIp * @return */ public static String get ...
- C#调用百度高精度IP定位API通过IP获取地址
API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...
- java工具类,在Windows,Linux系统获取电脑的MAC地址、本地IP、电脑名
package com.cloudssaas.util; import java.io.BufferedReader; import java.io.IOException; import java. ...
- 获取客户端网卡MAC地址和IP地址实现JS代码
获取客户端网卡MAC地址和IP地址实现JS代码 作者: 字体:[增加 减小] 类型:转载 获取客户端的一些信息,如IP和MAC,以结合身份验证,相信很多人都会这样做吧,我们这里用Javascrip ...
- c#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名.MAC地址.IP地址.硬盘ID.CPU序列号.系统名称.物理内存等. 首先需要引入命名空间: us ...
- ASP.NET根据IP获取省市地址
1.在网站的跟路径下面添加 QQWry.dat 文件,这个文件是IP数据库文件 2.添加以下一个类 IPScanner C# 代码 复制 public class IPScanner { ...
- js获取本机mac地址,IP地址,计算机名
<!DOCTYPE HTML> <html> <head> <title>js获取本机mac地址,IP地址,计算机名</title> < ...
- 如何根据Ip获取地址信息--Java----待整理完善!!!
如何根据Ip获取地址信息--Java----待整理完善!!! QQWry.dat数据写入方法: http://www.cnblogs.com/xumingxiang/archive/2013/02/1 ...
- java 通过ip获取客户端mac地址
java 通过ip获取客户端mac地址 package com.asppro.util; import java.io.BufferedReader; import java.io.IOExcepti ...
随机推荐
- error C2275: 'SOCKET' : illegal use of this type as an expression
在VC中编译xxx.c文件出现错误error C2275 illegal use of this type as an expression 问题在于C99之前要求所有的声明必须放在函数块的起始部分, ...
- 【转】理清基本的git(github)流程
概述 当我初次接触git时,我需要快速学习基本的git工作流,以便快速接收一个开源Web项目维护.但是,我很难理解工作流程,因为我不太了解git使用关键点. fork,clone,pull.branc ...
- mysqldump对于DB进行逻辑备份的时候,是否会备份视图呢?
需求描述: 今天在mysql备份的书的内容,提到了mysqldump在备份数据库的时候,不会备份视图 所以,就做了个实验测试下,发现,是能够备份视图的,在此记录下. 环境描述: Mysql版本:5.5 ...
- 《ASP.NET MVC4 WEB编程》学习笔记------乐观锁和悲观锁
摘要:对数据库的并发访问一直是应用程序开发者需要面对的问题之一,一个好的解决方案不仅可以提供高的可靠性还能给应用程序的性能带来提升.下面我们来看一下Couchbase产品市场经理Don Pinto结合 ...
- jquery获取父级元素、子级元素、兄弟元素的方法
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...
- MVC Route路由
由于某些原因,需要默认区域,所以需要对路由进行设置 具体实现如下: using System.Web; using System.Web.Mvc; using System.Web.Routing; ...
- String.Join重载String.Join 方法 (String, String[], Int32, Int32)
https://msdn.microsoft.com/zh-cn/library/tk0xe5h0 String.Join 方法 (String, String[], Int32, Int32) 官方 ...
- 【go】go语言socket通信样例
server.go package main import ( "net" "fmt" "io" ) func main() { liste ...
- 【thinkphp5】 分页样式修改
1 找到文件:/thinkphp/library/think/paginator/driver/Bootstrap.php <?php // +------------------------- ...
- LeetCode——Basic Calculator
Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...