HttpClient
라이브러리를 이용해 REST API를 호출하거나, HTML 코드를 가져오기도 합니다. 그런데 요청 URL이 리다이렉트(Redirect)가 되어 URL이 바뀌기도 합니다. 이렇게 Redirect 되면서 주소가 바뀌는 URL의 마지막으로 Redirect되는 URL를 구하는 방법을 작성합니다.
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
HttpHost target = context.getTargetHost();
List<URI> redirectLocations = context.getRedirectLocations();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
System.out.println("Final HTTP location: " + location.toASCIIString());
} finally {
response.close();
}
'Programming > JAVA' 카테고리의 다른 글
애니메이션 이미지인지 아닌지 체크하는 방법(Check image animated or not in JAVA) (0) | 2022.09.16 |
---|---|
[BufferedImage] 이미지 이진화(Image Binarize) (0) | 2022.09.16 |
[Tesseract] 이미지에서 텍스트 추출하기 (OCR) (0) | 2022.09.14 |
[POI] 대용량 엑셀 다운로드 SXSSFWorkbook (0) | 2022.08.18 |
[OpenCSV] Output Stream에 OpenCSV 쓰는 방법 (0) | 2022.08.18 |