Home > Pemrograman Java, Sistem Terdistribusi > Mengakses Web Server dengan URL

Mengakses Web Server dengan URL


Contoh di bawah ini mengakses web server menggunakan kelas URL dari paket .net. Sama sebagaimana contoh pemanfaatan kelas socket, kode ini menampilkan isi (dari sisi client) dari halaman HTML yang diakses:


import java.net.*;
import java.io.*;

public class GetURLPage {

    public static void main(String args[]) {

        try {
            URL url = new URL("http://localhost/");

            BufferedReader dis = new BufferedReader(new InputStreamReader(url.openStream()));

            String line;
            while ((line = dis.readLine()) != null) {
                System.out.println(line);
            }

            dis.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
} // akhir dari GetURLPage.java

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: