1 InputStream is = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip").openStream(); 2 BufferedReader rd = new BufferedReader(InputStreamReader(is, Charset.forName("UTF-8")));
上圖可能出現 超時的情況。
1 URL url = new URL("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=" + ip); 2 HttpURLConnection htpcon = (HttpURLConnection) url.openConnection(); 3 htpcon.setRequestMethod("GET"); 4 htpcon.setDoOutput(true); 5 htpcon.setDoInput(true); 6 htpcon.setUseCaches(false); 7 htpcon.setConnectTimeout(1000); 8 htpcon.setReadTimeout(1000); 9 InputStream in = htpcon.getInputStream(); 10 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
其實url的openStream就是把openConnection和getInputStream連起來調用了。