-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpsURLConnectionExample.java
More file actions
53 lines (41 loc) · 1.63 KB
/
HttpsURLConnectionExample.java
File metadata and controls
53 lines (41 loc) · 1.63 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package practice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpsURLConnectionExample {
public static void main(String[] args) {
try {
// API 요청을 보낼 URL
String apiUrl = "https://koreanjson.com/posts";
// URL 객체 생성
URL url = new URL(apiUrl);
// HttpsURLConnection 생성
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 요청 메서드 설정 (GET 요청)
connection.setRequestMethod("GET");
// 응답 코드 확인
int responseCode = connection.getResponseCode();
System.out.println("응답 코드: " + responseCode);
// 응답 데이터 읽기
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 응답 데이터 출력
System.out.println("응답 데이터:\n" + response.toString());
} else {
System.out.println("API 요청 실패");
}
// 연결 종료
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}