How to get HTTP response code for a URL in Java?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Getting the HTTP status code for a URL in Java is straightforward, but the preferred API depends on your Java version. On modern JDKs, HttpClient is the cleanest option. On older codebases, HttpURLConnection is still common.
Modern Java: HttpClient
If you are on Java 11 or later, use java.net.http.HttpClient.
BodyHandlers.discarding() is useful when you only need the status and do not want to store the response body.
Legacy-Compatible Java: HttpURLConnection
If you are working on older Java versions, HttpURLConnection still works.
This is still widely seen in existing codebases, but for new code HttpClient is usually better.
Use HEAD When You Only Need Status
If the server supports it, HEAD can be lighter than GET because it asks for headers without the body.
Modern Java:
That said, not every endpoint handles HEAD correctly. If you get strange behavior, fall back to GET.
Redirect Policy Matters
Some URLs reply with redirects rather than a final content status. Decide whether you want the original redirect code or the status after following the redirect chain.
With HttpClient, that behavior is configurable:
If you leave redirect handling unspecified, your observed status code may not match your operational expectation.
Distinguish HTTP Errors from Network Errors
This is an important design point:
- '
404,500, and503are valid HTTP responses' - DNS failures, timeouts, and SSL problems are exceptions
A method that fetches status codes should normally return the HTTP status for valid responses and only throw when the request itself could not be completed.
That distinction makes retry logic and observability much cleaner.
Check Ranges, Not Only 200
Many valid endpoints return statuses other than 200, such as:
- '
201 Created' - '
204 No Content' - '
301 Moved Permanently'
So your application logic may want range checks:
Or for broader health checks:
The correct rule depends on your use case.
Async Example
If you are checking many URLs, asynchronous requests can help reduce thread blocking.
This is useful for monitoring or batched endpoint probes.
Common Pitfalls
Forgetting timeouts can make the program hang far longer than intended on slow or broken networks.
Treating only 200 as success can reject perfectly valid responses such as 204.
Confusing transport failures with HTTP status failures makes logging and retry behavior harder to reason about.
Using HEAD everywhere without checking server support can lead to misleading failures on endpoints that do not implement it properly.
Summary
- Use
HttpClienton Java 11 and later for the cleanest status-code lookup. - Use
HttpURLConnectiononly when you need older-runtime compatibility. - Set explicit connect and read timeouts.
- Use
BodyHandlers.discarding()if you only need headers and status. - Treat HTTP error codes and network exceptions as different categories of failure.
Related reading
- How to get IP address of the device from code?
- How to get json response using system.net.webrequest in c?
- How to get Kubernetes cluster name from K8s API
- How to get mac host IP address from a docker container?
- How to get InputStream via Spring-Feign?
- How to get kafka consume lag in java program
- How to get my IP address programmatically on iOS/macOS?
- How to get Python requests to trust a self signed SSL certificate?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.