Using the URLConnection class

2023. 2. 14. 12:47자바

서버에 접근하는 간단한 방법! URLConnection class를 사용하는 것입니다.

 

이 클래스는 어플리케이션과 URL instance사이를 연결해줍니다.

 

try {
        URL url = new URL("http://www.google.com");
        URLConnection urlConnection = url.openConnection();
        BufferedReader br = new BufferedReader
                           ( new InputStreamReader(urlConnection.getInputStream()) );
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);}
        br.close();
        }
catch (IOException ex) {
        // Handle exceptions
        }

여기서 URL instance는 구글 웹사이트로 만들어집니다.

URL 클래스의 openConnection method를 사용하여 URLConnection instance가 생성됩니다.

BufferedReader instance는 이후 표시되는 연결에서 줄을 읽는 데 사용됩니다.

 

출력은 다소 길기에 첫줄의 일부만 표시됩니다.

URLConnection class는 액세스의 복잡함을 일부 숨깁니다.

 

 

'자바' 카테고리의 다른 글

Creating a simple echo server  (0) 2023.02.14
The client/server architecture  (0) 2023.02.14
Using the URLConnection class withbuffers and channels  (0) 2023.02.14
NIO support  (0) 2023.02.14
Network addressing using the InetAddress class  (0) 2023.02.14