Post

자바 HTTP URL 호출

1. 자바 HTTP URL 호출

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Slf4j
public class HttpUrlConnectionTest {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://dejavuhyo.github.io/");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                log.info(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

[출처 및 참고]

This post is licensed under CC BY 4.0 by the author.