通過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的地址:
【0分下載Demoproject】
通過IP獲取對應所在地的地址