What is URL and URLConnection in Java?
URL and URLConnection are Java networking classes used to access and communicate with resources available on the internet or network.
In simple words:
URL identifies the location of a web resource, while URLConnection helps establish communication with that resource.
Why URL and URLConnection are Important?
Modern applications constantly communicate with:
- REST APIs
- Web services
- Cloud platforms
- Payment gateways
- Microservices
- File servers
- External systems
URL and URLConnection Overview Diagram
Java Application
|
v
URL Object
|
v
URLConnection Opened
|
v
Remote Server/API
|
v
Data Exchange Happens
What is URL in Java?
URL stands for:
Uniform Resource Locator
It represents the address of a resource available on the internet or network.
Example URL
https://www.google.com/search
URL Components
| Component | Example |
|---|---|
| Protocol | https |
| Host | www.google.com |
| Port | 443 |
| Path | /search |
| Query Parameters | ?q=java |
URL Structure Diagram
https://www.google.com:443/search?q=java | | | | | | | | Protocol Host Path Query
Main Package
java.net
How to Create URL Object?
URL url =
new URL(
"https://www.google.com"
);
Useful URL Methods
| Method | Purpose |
|---|---|
| getProtocol() | Returns protocol |
| getHost() | Returns host name |
| getPort() | Returns port number |
| getPath() | Returns path |
| getQuery() | Returns query string |
URL Example
URL url =
new URL(
"https://www.google.com/search?q=java"
);
System.out.println(
url.getProtocol()
);
System.out.println(
url.getHost()
);
System.out.println(
url.getPath()
);
System.out.println(
url.getQuery()
);
Output
https www.google.com /search q=java
What is URLConnection?
URLConnection is a class used to establish communication between Java applications and remote resources identified by URLs.
URLConnection Overview
URL Created
|
v
openConnection() Called
|
v
URLConnection Established
|
v
Data Transfer Happens
How to Open URLConnection?
URL url =
new URL(
"https://example.com"
);
URLConnection connection =
url.openConnection();
Main Uses of URLConnection
- Read web page content
- Connect to REST APIs
- Download files
- Send requests
- Read headers
- Access remote systems
Reading Data Using URLConnection
URL url =
new URL(
"https://example.com"
);
URLConnection connection =
url.openConnection();
BufferedReader br =
new BufferedReader(
new InputStreamReader(
connection.getInputStream()
)
);
String line;
while(
(line = br.readLine()) != null
) {
System.out.println(line);
}
br.close();
Internal Working Flow
URL Object Created
|
v
URLConnection Opened
|
v
InputStream Retrieved
|
v
Data Read From Server
What Happens Internally?
- DNS lookup happens
- Socket connection created
- HTTP/HTTPS request sent
- Server response received
- InputStream returns data
URLConnection Lifecycle
URL
|
v
URLConnection
|
v
Connect to Server
|
v
Request Sent
|
v
Response Received
Common URLConnection Methods
| Method | Purpose |
|---|---|
| connect() | Opens connection |
| getInputStream() | Reads response data |
| getOutputStream() | Sends data |
| getContentType() | Returns MIME type |
| getContentLength() | Returns response size |
Example Using connect()
URLConnection connection =
url.openConnection();
connection.connect();
Difference Between URL and URLConnection
| Feature | URL | URLConnection |
|---|---|---|
| Purpose | Represents Resource Address | Creates Communication |
| Package | java.net | java.net |
| Communication | No | Yes |
| Used For | Resource Identification | Data Transfer |
| Examples | Website Address | Downloading Web Content |
HTTPURLConnection
HTTPURLConnection is a subclass of URLConnection specifically for HTTP communication.
Example
HttpURLConnection connection =
(HttpURLConnection)
url.openConnection();
Why HTTPURLConnection Important?
It supports:
- GET requests
- POST requests
- Headers
- Status codes
- Timeouts
HTTP GET Request Example
HttpURLConnection connection =
(HttpURLConnection)
url.openConnection();
connection.setRequestMethod(
"GET"
);
int code =
connection.getResponseCode();
HTTP Request Flow
Java Application
|
v
HTTP Request Sent
|
v
Server Processes Request
|
v
HTTP Response Returned
Reading HTTP Response
BufferedReader br =
new BufferedReader(
new InputStreamReader(
connection.getInputStream()
)
);
URLConnection in Banking Systems
Banking applications use URL and URLConnection for:
- Payment gateway integration
- Third-party API communication
- Fraud detection APIs
- Transaction verification
- Cloud integrations
Banking Flow
Banking Application
|
v
URLConnection Sends API Request
|
v
Payment Gateway Processes Request
|
v
Secure Response Returned
URLConnection in E-Commerce Systems
E-commerce platforms use them for:
- Payment APIs
- Shipping integrations
- Inventory synchronization
- External vendor APIs
- Currency conversion APIs
E-Commerce Flow
Order Created
|
v
URLConnection Calls Shipping API
|
v
Tracking Information Returned
URLConnection in Spring Boot
Spring Boot applications use URLConnection concepts internally through:
- RestTemplate
- WebClient
- Feign clients
- HTTP clients
- External API integrations
Spring Boot REST Flow
Spring Service Calls External API
|
v
URLConnection/HTTP Client Used
|
v
Response Converted into Objects
URLConnection in Microservices
Microservices architectures use URLConnection concepts for:
- Service-to-service communication
- Distributed APIs
- Cloud-native integrations
- API gateways
- External service orchestration
Microservice Flow
Service A Creates URL
|
v
URLConnection Established
|
v
REST Request Sent
|
v
Response Returned to Service A
Advantages of URL and URLConnection
- Simple networking API
- Supports internet communication
- Easy API integration
- Supports multiple protocols
- Widely used in enterprise systems
Disadvantages
- Low-level API
- More boilerplate code
- Less flexible than modern HTTP clients
- Harder asynchronous support
Common Interview Mistake
Many developers think URL downloads data directly.
Actually:
- URL only identifies resource location.
- URLConnection performs communication.
Another Common Mistake
Many developers think URLConnection only supports websites.
Actually:
- URLConnection supports multiple protocols like HTTP, HTTPS, FTP, and file resources.
Best Practices
- Close streams properly
- Use try-with-resources
- Handle timeouts carefully
- Validate URLs securely
- Use HTTPS for secure communication
- Prefer modern clients like WebClient for reactive systems
Realtime Enterprise Example
Payment Gateway Integration System
Customer Makes Payment
|
v
Application Creates Payment API URL
|
v
URLConnection Sends Request
|
v
Payment Gateway Responds
|
v
Transaction Status Updated
Related Learning Topics
- What is HttpURLConnection in Java
- What is Socket Programming in Java
- What is TCP and UDP in Java Networking
- What is Java NIO
- Difference Between URL and URI in Java
- What are Microservices
Professional Interview Answer
URL (Uniform Resource Locator) in Java represents the address of a resource available on the internet or network, while URLConnection is a networking class used to establish communication with that resource and transfer data. The URL class helps identify components such as protocol, host, port, path, and query parameters, whereas URLConnection provides methods for opening connections, sending requests, reading responses, handling headers, and communicating with remote systems. Java also provides HttpURLConnection for HTTP-specific communication such as GET and POST requests. Enterprise applications, Spring Boot systems, banking platforms, distributed microservices, cloud-native architectures, payment gateways, REST APIs, e-commerce systems, and external service integrations heavily use URL and URLConnection concepts for API communication, remote resource access, distributed networking, and service orchestration. Modern frameworks such as RestTemplate, WebClient, Feign clients, and reactive HTTP clients internally build upon these networking foundations.
Frequently Asked Questions
What is URL in Java?
URL represents the address of a resource on the internet or network.
What is URLConnection in Java?
URLConnection establishes communication with resources identified by URLs.
What is the difference between URL and URLConnection?
URL identifies the resource, while URLConnection performs communication.
What is HttpURLConnection?
It is a subclass of URLConnection used specifically for HTTP communication.
Where are URL and URLConnection used?
REST APIs, payment gateways, Spring Boot applications, microservices, and cloud integrations.