通过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 {
	
	/**
	 * 对服务器端返回的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"));//输出结果:辽宁省大连市 联通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所属地的代码比较多,这里仅给出下载该java工程的地址:

        【0分下载Demo工程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿老高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值