เขียน Java อ่านข้อมูลจากเว็บไซต์

August 24, 2008 by admin
Filed under: Java 

ตัวอย่างซอร์สโค้ดสำหรับการดึงข้อมูลจากเว็บไซต์ ซึ่งมีการระบุ URL ที่ต้องการอ่าน

try {
// กำหนด URL ที่ต้องการอ่านข้อมูล
URL url = new URL("http://www.devfeel.com/index.php");

// อ่านข้อมูลทั้งหมด
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str  เป็นข้อมูลที่อ่านได้จากเว็บไซต์ ในแต่ละบรรทัด
System.out.println(str);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!