/** * * @param ipaddr * @return public ip */String GetNetIp(String ipaddr){ URL infoUrl = null; InputStream inStream = null; try { infoUrl = new URL(ipaddr); URLConnection connection = infoUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; int responseCode = httpConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line ); inStream.close(); return strber.toString(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }
傳入的地址可以是
http://automation.whatismyip.com/n09230945.asp