Java 程式設計 -- 網路
Java 程式簡介運算式分枝迴圈陣列函數遞迴錯誤處理物件導向封裝繼承多型技巧函式庫字串數學正規表達式容器檔案網路資料庫視窗ThreadListener錯誤陷阱相關檔案相關資源教學錄影Eclipse考題解答訊息相關網站參考文獻最新修改簡體版English |
範例一:下載一個網頁import java.util.Properties; import java.io.*; import java.net.*; public class Net1 { public static void main(String[] args) throws Exception { // setProxy("proxy.internal", "3128"); // 若你在金門大學裡面執行本程式,請加上本行 ! String html = download("http://tw.yahoo.com/"); System.out.println(html); } public static String download(String pUrl) throws Exception { StringBuffer rzText = new StringBuffer(); URL url = new URL(pUrl); InputStream in = url.openStream(); try { byte[] buffer = new byte[1024]; while (true) { int len = in.read(buffer); rzText.append(new String(buffer, 0, len)); } } catch (Exception e) {} in.close(); return rzText.toString(); } public static void setProxy(String pProxy, String pPort) { Properties systemSettings = System.getProperties(); systemSettings.put("proxySet", "true"); systemSettings.put("proxyHost", pProxy); systemSettings.put("proxyPort", pPort); System.setProperties(systemSettings); } } |
page revision: 0, last edited: 04 Nov 2010 03:26
Post preview:
Close preview