通过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 ...
随机推荐
- JavaScript编码encode和decode escape和unescape
encodeURI() 函数可把字符串作为 URI 进行编码. 语法 encodeURI(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 或其他要编码的文本. 返 ...
- dedecms的arclist循环中判断第一个li添加css,否则不加
dedecms的arclist循环中,判断如果是第一个li,则添加固定的css,否则不加 写法如下: {dede:arclist row=4 flag='p'} <li [field:glo ...
- Landpy.ActiveDirecoty,按照Active Record Pattern设计的方便Lib开源发布
想方便的操作AD吗,不想记住那么多AD Attribute名称和常量?请使用Landpy.ActiveDirecoty库,按照Active Record Pattern设计的AD Lib已经在Code ...
- C语言中do...while(0)的妙用-避免goto
使用goto的优雅并避免结构的混乱 将要跳转到的语句用do{-}while(0) 包起来就可以. reference #defien N 10 bool Execute() { // 分配资源 int ...
- Android代码调试报错
最近出现老代码不能运行的问题 Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowExc ...
- laravel 使用 session
配置方面的不写了,请参考学院君的文章:http://laravelacademy.org/post/5898.html 在开始之前先说一下,使用 request 对象的 session() 方法,和直 ...
- mybatis 循环遍历
/****Service/ public ServiceMessage<MemberFreedomRepModel> getMFListByPay(Long memberId,Long f ...
- JS常用函数与方法
//当页面关闭时触发 window.onbeforeunload = function() { alert('关闭了吧'); } //关闭窗口(弹出式窗口) parent.window.close() ...
- LeetCode——Number of 1 Bits
//求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...
- js:{}与new Object()的区别是什么
var a = {}; var b = new Object(); 这两种创建对象方式,从测试效果来看,{}会快一点. {} 这个叫做对象字面量 如果new Object()中没有传入参数,与{}是一 ...