Post

HttpClient Deepdive

HttpClient Deepdive

๐Ÿ”” BodyPublisher, BodyHandler ์—ญํ• 
๐Ÿ”” HttpClient ์‹ค์Šต

BodyPublisher? BodyHandler?

BodyPublisher์™€ BodyHandlers๋Š” ์š”์ฒญ๊ณผ ์‘๋‹ต์˜ ๋ณธ๋ฌธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์žˆ์–ด ์ƒํ˜ธ ๋ณด์™„์ ์ธ ์—ญํ• ์„ ํ•œ๋‹ค. BodyPublisher๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ , BodyHandlers๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€๋ฅผ ์ •์˜ํ•œ๋‹ค. ์ด ๋‘ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜๋ฉด, HTTP ํ†ต์‹ ์„ ๋”์šฑ ์œ ์—ฐํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

BodyPublisher

  • HTTP ์š”์ฒญ์˜ ๋ณธ๋ฌธ์„ ์ œ๊ณต
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  ๋•Œ ์–ด๋–ป๊ฒŒ ์ œ๊ณตํ• ์ง€ ์ •์˜
    • BodyPublishers.ofString(String): ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ
    • BodyPublishers.ofByteArray(byte[]): ๋ฐ”์ดํŠธ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ
    • BodyPublishers.ofFile(Path): ํŒŒ์ผ ๋ฐ์ดํ„ฐ
    • BodyPublishers.ofInputStream(Supplier<InputStream>): ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ

BodyHandler

  • HTTP ์‘๋‹ต์˜ ๋ณธ๋ฌธ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •์˜
  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ• ์ง€ ์ •์˜
    • BodyHandlers.ofString(): ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ
    • BodyHandlers.ofByteArray(): ๋ฐ”์ดํŠธ ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ
    • BodyHandlers.ofFile(Path): ํŒŒ์ผ ๋ฐ์ดํ„ฐ
    • BodyHandlers.ofInputStream(): InputStream์œผ๋กœ ๋ฐ์ดํ„ฐ ์ œ๊ณต

HttpResponse.BodyHandler ํด๋ž˜์Šค๋Š” BodyHandler๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ํŽธ๋ฆฌํ•œ ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด๋“ค ์ค‘ ๋‹ค์ˆ˜๋Š” ์‘๋‹ต ๋ฐ”์ดํŠธ๊ฐ€ ์™„์ „ํžˆ ์ˆ˜์‹ ๋  ๋•Œ๊นŒ์ง€ ๋ฉ”๋ชจ๋ฆฌ์— ์ถ•์ ๋˜๋ฉฐ, ๊ทธ ํ›„ ์‘๋‹ต ๋ฐ”์ดํŠธ๋Š” ์ƒ์œ„ ์ˆ˜์ค€์˜ Java ์œ ํ˜•(ofString, ofByteArray ๋“ฑ)์œผ๋กœ ๋ณ€ํ™˜๋œ๋‹ค.

Get

๋™๊ธฐ

  • BodyHandler๋Š” ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ์™€ ํ—ค๋”๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์ง€๋ฉด ์‘๋‹ต ๋ณธ๋ฌธ ๋ฐ”์ดํŠธ๊ฐ€ ์ˆ˜์‹ ๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ
1
2
3
4
5
6
7
8
9
10
public void get(String uri, String param) throws Exception {
    String uriWithParam = uri+"?"+param;
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(uriWithParam))
            .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println("Sync status code: "+ response.statusCode());
}

๋น„๋™๊ธฐ

  • ๋น„๋™๊ธฐ API๋Š” ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ด์ง€๋ฉด HttpResponse๋กœ ์™„๋ฃŒ๋˜๋Š” CompletableFuture๋ฅผ ์ฆ‰์‹œ ๋ฐ˜ํ™˜
  • CompletableFuture.thenApply(Function) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HttpResponse๋ฅผ ๋ณธ๋ฌธ ์œ ํ˜•, ์ƒํƒœ ์ฝ”๋“œ ๋“ฑ์— ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • join(): ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋๋‚  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
public void getAsync(String uri, String param) {
    String uriWithParam = uri+"?"+param;
    HttpClient client = HttpClient.newHttpClient();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(uriWithParam))
            .build();

    client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .thenApply(HttpResponse::statusCode)
            .thenAccept(statusCode -> System.out.println("Async status code: " + statusCode))
            .join();
}

Post

  • discard BodyHandler๋Š” ๊ด€์‹ฌ์ด ์—†๋Š” ์‘๋‹ต ๋ณธ๋ฌธ์„ ์ˆ˜์‹ ํ•˜๊ณ  ์‚ญ์ œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.
1
2
3
4
5
6
7
8
9
10
public void post(String uri, String data) throws Exception {
    HttpClient client = HttpClient.newBuilder().build();
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(uri))
            .POST(HttpRequest.BodyPublishers.ofString(data))
            .build();

    HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.discarding());
    System.out.println("Post status code: " + response.statusCode());
}

์‹ค์Šต

์˜ˆ์ œ

๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ ํ˜ธ์ถœ์— ๋Œ€ํ•œ ์˜ˆ์ œ์ด๋‹ค. ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ์— ์ ์šฉ ์ค‘์ธ ์„ค์ •์œผ๋กœ requestUrl๊ณผ redirectUrl์€ ์ด๋ฏธ ์„ค์ •๋˜์–ด์žˆ๋Š” ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ–ˆ๋‹ค.
์‹ค์ œ ๊ฒฐ๊ณผ๋Š” ํŽ˜์ด์ง€๊ฐ€ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•˜๋ฉด์„œ text/html ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒํƒœ์ฝ”๋“œ๋งŒ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
    HttpClientExample httpClientExample = new HttpClientExample();
    String requestUrl = "https://nid.naver.com/oauth2.0/authorize";
    String redirectUrl = "https://4d4cat.site/login/naver/callback";
    String clientKey = "๋„ค์ด๋ฒ„ํ‚ค๊ฐ’";

    NaverLoginRequest requestParam = new NaverLoginRequest("code", clientKey, redirectUrl, "state");
    try {
        httpClientExample.get(requestUrl, requestParam.toString());
        httpClientExample.getAsync(requestUrl, requestParam.toString());
        httpClientExample.post(requestUrl, requestParam.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

์ถœ๋ ฅ

1
2
3
Sync status code: 200
Async status code: 200
Post status code: 200

๊ณต๋ถ€ํ•ด์•ผํ•˜๋Š” ๊ฐœ๋…

๊นƒํ—ˆ๋ธŒ

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