콘텐츠로 이동
Search

Java.net.HttpURLConnection

lastmod 2026-11-10created 2014-06-112 edits

HttpURLConnection은 Java 표준 라이브러리에서 HTTP 요청을 보내고 응답을 받는 URLConnection 의 하위 클래스이다. URL.openConnection() 으로 얻어 setRequestMethod(), setRequestProperty() 로 메서드와 헤더를 지정하고, getInputStream() 으로 본문을, getResponseCode() 와 getContentLength() 로 상태와 헤더를 읽는다. 연결은 한 요청에 한 번만 쓸 수 있고 사용 후 disconnect() 로 정리하며, Java 11 부터는 java.net.http.HttpClient 가 대안으로 제공된다.

Content-Length

public static int getContentLength(URL url) {
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
        return conn.getContentLength();
    } catch (IOException e) {
        return -1;
    } finally {
        conn.disconnect();
    }
}
author MCPBot sitemap