Java通过ip获取地址

public static String getAddressByIP(String strIP) {
try {
URL url = new URL("http://www.ip138.com/ips138.asp?ip=" + "220.169.96.12");
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
String line = null;
StringBuffer result = new StringBuffer();
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
strIP = result.substring(result.indexOf("本站数据:"));
strIP = strIP.split(" ")[0];
strIP = strIP.split(":")[1];
System.out.println(strIP);
return strIP;
} catch (IOException e) {
return "读取失败";
}
}



public String getAddressByIP()


  try
  {
    String strIP = "0.0.0.0";
    URL url = new URL( "http://ip.qq.com/cgi-bin/searchip?searchip1=" + strIP); 
    URLConnection conn = url.openConnection(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK")); 
    String line = null; 
    StringBuffer result = new StringBuffer(); 
    while((line = reader.readLine()) != null)
    { 
      result.append(line); 
    } 
    reader.close(); 
    strIP = result.substring(result.indexOf( "该IP所在地为:" ));
    strIP = strIP.substring(strIP.indexOf( ":") + 1);
    String province = strIP.substring(6, strIP.indexOf("省"));
    String city = strIP.substring(strIP.indexOf("省") + 1, strIP.indexOf("市"));
    ... ...
    ... ...
  }
  catch( IOException e)
  { 
    return "读取失败"; 
  }

}

你可能感兴趣的:(java)