본문 바로가기

개발/Spring Frame Work

24-08-09기준 apache httpComponts(httpmime4 to 5) migration

httpComponts(mime)에서를 5버전으로 migration하는 과정에서 발생되는 소스코드 수정부분에 대한 내역

# builder 상세정보

- maven(pom.xml) [ 4 -> 5 ]

# version 4
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.14</version>
</dependency>

# version 5
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.3.1</version>
</dependency>

- gradle(build.gradle) [ 4 -> 5]

# version 4
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime
implementation 'org.apache.httpcomponents:httpmime:4.5.14'

# version 5
// https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5
implementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1'

 

# java source code sample ( version 4 )

URI uri = new URI(url);
HttpGet httpGet = new HttpGet(uri);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(60000) .setSocketTimeout(100000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
HttpResponse response = httpClient.execute(httpGet);
String reponseText = EntityUtils.toString(response.getEntity());

 

# java source code sample ( version 5 )

- 5버전에서는 execute 관련 deprecated 된 사항이 많아 사용방식이 많이 변경

- syso는 logback이나 log4j로 변경하도록.

# 방식 1

String url = "http://example.com";
URI uri = new URI(url);
HttpGet httpGet = new HttpGet(uri);
RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(Timeout.ofSeconds(6)) .setResponseTimeout(Timeout.ofSeconds(10)) .build();
try (CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(requestConfig) .build()) {
  ResponseHandler<String> responseHandler = response -> {
    int status = response.getCode();
    if (status >= 200 && status < 300) {
      HttpEntity entity = response.getEntity();
      return entity != null ? EntityUtils.toString(entity) : null;
    } else {
      throw new ClientProtocolException("Unexpected response status: " + status);
    }
  };
  String responseText = httpClient.execute(httpGet, responseHandler);
  System.out.println(responseText);
}

# 방식 2

String url = "http://example.com";
URI uri = new URI(url);
HttpGet httpGet = new HttpGet(uri);
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(Timeout.ofSeconds(6)) .setResponseTimeout(Timeout.ofSeconds(10)).build();
httpGet.setConfig(requestConfig);
String reponseText = "";
try (CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(requestConfig) .build()) {
  reponseText = httpClient.execute(httpGet, response -> {
    int status = response.getCode();
    if (status >= 200 && status < 300) {
      return EntityUtils.toString(response.getEntity());
    } else {
      throw new HttpResponseExceptionException(status, "Unexpected response status: " + status);
    }
  });
} catch (IOException e) {
  System.out.println(e.getMessage());
}
System.out.println(reponseText);

참고할수 있는 샘플 코드는 공식사이트 에서 확인하여 본인 사이트에 맞게 활용하여 개발 진행.(reference 참조) 

# reference

- https://hc.apache.org/

 

Apache HttpComponents – Apache HttpComponents

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apa

hc.apache.org

- https://github.com/apache/httpcomponents-client/blob/5.3.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/

 

httpcomponents-client/httpclient5/src/test/java/org/apache/hc/client5/http/examples at 5.3.x · apache/httpcomponents-client

Mirror of Apache HttpClient. Contribute to apache/httpcomponents-client development by creating an account on GitHub.

github.com