程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> 獲取非局域網的ip地址

獲取非局域網的ip地址

編輯:關於JSP

[java] 
/**
     * 獲取非局域網的ip地址
     *
     * @return
     * @author SHANHY
     */ 
    public static String getPsdnIp() { 
        String ipurl = "www.2cto.com"; 
        String ss =null; 
        HttpURLConnection urlConnection = null; 
        try { 
            urlConnection = null; 
            try { 
                URL url = new URL(ipurl); 
                urlConnection = (HttpURLConnection) url.openConnection(); 
                urlConnection.setDoInput(true); 
                urlConnection 
                        .setConnectTimeout(WoConfiguration.TIME_OUT_LENGTH); 
                urlConnection.setReadTimeout(WoConfiguration.TIME_OUT_LENGTH); 
 
                urlConnection.setRequestProperty("Content-Type", 
                        "application/x-www-form-urlencoded"); 
 
                urlConnection.connect(); 
 
                int response = urlConnection.getResponseCode(); 
                 
                if (response == HttpConnection.HTTP_OK) { 
                    InputStream in = urlConnection.getInputStream();// .openInputStream(); 
                    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                    byte[] buff = new byte[1024]; 
                    int rc = 0; 
                    while ((rc = in.read(buff, 0, 1024)) != -1) { 
                        baos.write(buff, 0, rc); 
                    } 
                    byte[] content = baos.toByteArray(); 
                    ss = new String(content); 
                    in.close(); 
                    in = null; 
                    baos.close(); 
                    baos = null; 
                    JSONObject sn=new JSONObject(ss.substring(ss.indexOf("({")+1,ss.indexOf("})")+1)); 
                    ss=sn.getString("ip"); 
                } else { 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
        } catch (Exception e) { 
            return null; 
        } finally { 
            if (urlConnection != null) { 
                urlConnection.disconnect();// .close(); 
                urlConnection = null; 
            } 
        } 
        return ss; 
    } 
作者:hehe9737

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved