HTTP 연결시 Request header에 Accept-Encoding = gzip 또는 deflate 로 지정이 되었을 경우(Accept-Encoding="gzip, deflate")

서버에서는 트래픽 양을 줄이기 위하여 컨텐츠를 모두 압축하여 보내준다.

물론 서버 설정에서 gzip으르 지원하지 않도록 설정을 바꿀 수 있지만 그렇게 되면 트래픽이 과도하게 발생된다.

어쨌건간에 Http로 연결을 한 후 gzip으로 인코딩이 되었다면 Java에서 GZIPInputStream 으로 간단히 해결 할 수 있다.

 

 

 

 

        URL url = new URL("http://[도메인]/[서비스경로]");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);

        

        connection.setRequestProperty("Host", "tops.tobesoft.com");

        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");

        connection.setRequestProperty("Referer", "http://[도메인]/[서비스경로]/[서비스페이지]");

        connection.setRequestProperty("Accept", "application/xml, text/xml, */*");

        connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); //이 코드가 들어가면 서버는 응답데이터를 압축해서 보내준다

        connection.setRequestProperty("Content-type", "text/xml;charset=UTF-8");

        connection.setRequestProperty("Pragma", "no-cache");

        connection.setRequestMethod("POST");

        connection.setDoOutput(true);

 

        OutputStream out_stream = connection.getOutputStream();

        out_stream.write( trData.getBytes("UTF-8") );

        out_stream.flush();

        out_stream.close();

        

        connection.connect();

        

        String sessCookie = connection.getHeaderField("Set-Cookie");

        if (sessCookie != null) {

            System.out.println("쿠키:"+sessCookie);

        }

        

        BufferedReader brin = null;

        String cEncode = connection.getHeaderField("Content-Encoding");

        if (cEncode != null &&

                (cEncode.toLowerCase().indexOf("gzip") > -1 

                || cEncode.toLowerCase().indexOf("deflate") > -1

                || cEncode.toLowerCase().indexOf("compress") > -1)) {

            brin = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream())));

        } else {

            brin = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));

        }

        

        String resStr = null;

        StringBuffer resStrBuff = new StringBuffer();

        try {

            while ((resStr = brin.readLine()) != null) {

                resStrBuff.append(resStr);

            }

        } finally {

            if (brin != null) {

                brin.close();

                brin = null;

            }

            connection = null;

            url = null;

            resStr = null;

        }

번호 제목 글쓴이 날짜 조회 수
347 java.util.Optional 에서의 map() 함수의 이해 황제낙엽 2026.06.24 666
346 linux(ubuntu) 의 jdk 21 이 설정된 환경에서 jdk 17 로 다운그레이드 절차 황제낙엽 2025.09.22 1532
345 [ChatGPT] javax.* 패키지와 jakarta.* 패키지의 혼합 사용 방안 황제낙엽 2025.04.15 2459
344 [ChatGPT] *.servlet.http.HttpServlet 클래스를 사용할 수 있는 주요 WAS 목록 황제낙엽 2025.04.14 2038
343 [ChatGPT] JDBC API에서 java.sql과 javax.sql 패키지의 차이점 황제낙엽 2025.01.31 2218
342 jakarta.servlet 패키지를 이용한 File Upload 구현 (2024.03.03, Servlet 5.0) 황제낙엽 2025.01.02 2310
341 [ChatGPT] Java EE가 Jakarta EE로 전환 황제낙엽 2025.01.01 2525
340 [ChatGPT] File.delete() 함수로 파일 삭제가 되지 않는 경우 황제낙엽 2024.12.10 2809
339 [ChatGPT] <jsp:include>에서 flush="true"를 설정시의 몇 가지 문제점 과 대안 황제낙엽 2024.07.19 2493
338 [Copilot] JSP, Servlet, web.xml, mysql 한글 설정 황제낙엽 2024.07.12 2555
337 Base64 클래스의 encode, decode 에서 URL-safe 황제낙엽 2024.07.10 2624
336 [Copilot] JSP 에서 다른 페이지를 현재 페이지에 포함시키는 문법 (include) 황제낙엽 2024.06.26 2570
335 Exception 출력에 대한 고찰 황제낙엽 2024.06.10 2198
334 google-auth-library-oauth2-http 라이브러리 다운로드 황제낙엽 2023.11.19 4025
333 firebase-admin-java 라이브러리 다운로드 (firebase admin sdk library) 황제낙엽 2023.11.19 2805
332 Enum 활용 (개인블로그, Effective Java) file 황제낙엽 2023.11.02 2759
331 Enum 활용 (우아한기술블로그) file 황제낙엽 2023.11.02 2790
330 servlet name 에 camel case 를 사용해서는 안되는 이유 황제낙엽 2023.10.24 2508
329 org.apache.commons.fileupload.servlet.ServletFileUpload 이용 예시 황제낙엽 2023.08.25 3336
328 ServletFileUpload.parseRequest() 실행 결과 리스트가 비어있는 오류 황제낙엽 2023.08.25 2935