'accept'에 해당되는 글 1건

  1. 2019.11.05 [httpcomponents-client] CloseableHttpClient - Accept Encoding

[httpcomponents-client] CloseableHttpClient - Accept Encoding

ITWeb/개발일반 2019. 11. 5. 10:53

RESTful 통신을 많이 하면서 httpclient 를 활용이 높습니다.

제가 사용하고 있는 httpclient 중에 CloseableHttpClient 가 있는데 이 클라이언트의 경우 Accept Encoding 설정이 기본적으로 enable 되어 있습니다.

그래서 기억력을 돕기 위해 또 기록해 봅니다.

 

참고 이전 글)

https://jjeong.tistory.com/1369

 

HttpClientBuilder.java)

public CloseableHttpClient build() {
... 중략 ...
            if (!contentCompressionDisabled) {
                if (contentDecoderMap != null) {
                    final List<String> encodings = new ArrayList<String>(contentDecoderMap.keySet());
                    Collections.sort(encodings);
                    b.add(new RequestAcceptEncoding(encodings));
                } else {
                    b.add(new RequestAcceptEncoding());
                }
            }
            if (!authCachingDisabled) {
                b.add(new RequestAuthCache());
            }
            if (!cookieManagementDisabled) {
                b.add(new ResponseProcessCookies());
            }
            if (!contentCompressionDisabled) {
                if (contentDecoderMap != null) {
                    final RegistryBuilder<InputStreamFactory> b2 = RegistryBuilder.create();
                    for (final Map.Entry<String, InputStreamFactory> entry: contentDecoderMap.entrySet()) {
                        b2.register(entry.getKey(), entry.getValue());
                    }
                    b.add(new ResponseContentEncoding(b2.build()));
                } else {
                    b.add(new ResponseContentEncoding());
                }
            }
... 중략 ...
    }

RequestAcceptEndocing.java)

...중략...
    public RequestAcceptEncoding(final List<String> encodings) {
        if (encodings != null && !encodings.isEmpty()) {
            final StringBuilder buf = new StringBuilder();
            for (int i = 0; i < encodings.size(); i++) {
                if (i > 0) {
                    buf.append(",");
                }
                buf.append(encodings.get(i));
            }
            this.acceptEncoding = buf.toString();
        } else {
            this.acceptEncoding = "gzip,deflate";
        }
    }
...중략...

요청 하는 Client 에서 Server 로 콘텐츠를 압축해서 전송해줘 해야 압축해서 전송을 해주게 되는 내용입니다.

아무리 서버에서 압축 전송이 가능 하도록 설정을 했어도 요청을 하지 않으면 그냥 plain/text 로 넘어 올 수 밖에 없습니다.

 

참고문서)

https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding

 

Accept-Encoding

Accept-Encoding 요청 HTTP 헤더는, 보통 압축 알고리즘인, 클라이언트가 이해 가능한 컨텐츠 인코딩이 무엇인지를 알려줍니다. 컨텐츠 협상을 사용하여, 서버는 제안된 내용 중 하나를 선택하고 사용하며 Content-Encoding 응답 헤더를 이용해 선택된 것을 클라이언트에게  알려줍니다.

developer.mozilla.org

 

: