Java Socket Programming 大綱 - 網路資料庫實驗室
Java Socket Programming 大綱 - 網路資料庫實驗室
Java Socket Programming 大綱 - 網路資料庫實驗室
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
讀取URL資料<br />
100. import java.net.*;<br />
101. import java.io.*;<br />
102. public class URLReader {<br />
103. public static void main(String[] args) throws Exception {<br />
104. URL yahoo = new URL("http://www.yahoo.com/");<br />
105. BufferedReader in = new BufferedReader(<br />
106. new InputStreamReader(<br />
107. yahoo.openStream()));<br />
108. String inputLine;<br />
109. while ((inputLine = in.readLine()) != null)<br />
110. System.out.println(inputLine);<br />
111. in.close();<br />
112. }<br />
113. }<br />
URL連線錯誤處理<br />
100. try {<br />
<strong>Java</strong> <strong>Socket</strong> <strong>Programming</strong> 25<br />
101. URL yahoo = new URL("http://www.yahoo.com/");<br />
102. URLConnection yahooConnection =<br />
yahoo.openConnection();<br />
103. } catch (MalformedURLException e) { // new URL() failed<br />
104. . . .<br />
105. } catch (IOException e) { // openConnection() failed<br />
106. . . .<br />
107. }<br />
<strong>Java</strong> <strong>Socket</strong> <strong>Programming</strong> 26<br />
13